0

我想要使用在Obj-C中编写的自定义ActivityIndi​​catorView。它包含一个将指标集中在UIView框架中的方法。我在使用Swift代码时遇到了麻烦。下面是代码...如何转换为Swift的MONActivityView方法

- (void)placeAtTheCenterWithView:(UIView *)view { 
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view 
                  attribute:NSLayoutAttributeCenterX 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                  attribute:NSLayoutAttributeCenterX 
                 multiplier:1.0f 
                  constant:0.0f]]; 

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view 
                  attribute:NSLayoutAttributeCenterY 
                  relatedBy:NSLayoutRelationEqual 
                  toItem:self.view 
                  attribute:NSLayoutAttributeCenterY 
                 multiplier:1.0f 
                  constant:0.0f]]; 
} 

到GitHub上的链接https://github.com/mownier/MONActivityIndicatorView

我怎样才能在斯威夫特用这个?

回答

1

这是你在找什么?将objective-c转换为swift?如果是这样,这里是翻译:

func placeAtTheCenterWithView(view: UIView) { 
    let xCenterConstraint = NSLayoutConstraint(item: view, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0) 
    let yCenterConstraint = NSLayoutConstraint(item: view, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0) 
    self.view.addConstraints([xCenterConstraint, yCenterConstraint]) 
} 

干杯;)