如何转换Swift的MONActivityView方法



我正在尝试使用用Obj-C编写的自定义ActivityIndicatorView。它包含在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中使用此功能?

这就是您想要的吗?将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])
}

干杯;)

最新更新