如何为Swift转换MONActivityView方法

时间:2016-04-15 04:54:31

标签: swift custom-controls uiactivityindicatorview addsubview

我尝试使用Obj-C编写的自定义ActivityIndi​​catorView。它包含在UIView框架中居中指示器所需的方法。我在Swift代码中使用它时遇到了麻烦。这是代码......

- (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

如何在Swift中使用它?

1 个答案:

答案 0 :(得分: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])
}

干杯;)