ios - 如何创建一个具有创建可放置在不同位置的 UIView 的函数的扩展?

标签 ios swift swift-extensions

我必须创建一个自定义 UIView,它实际上是一个具有相同大小和外观的圆圈,放置在 View 中的不同位置。我创建了一个适用于一个圆的扩展,但我想使用相同的函数将圆放置在不同的位置。这是我的扩展:

extension UIView{

func helptip(hotSpot: HelpTips, parentView: UIView){
    hotSpot.tag = 1
    hotSpot.userInteractionEnabled = true
    hotSpot.backgroundColor = UIColor.clearColor()
    hotSpot.translatesAutoresizingMaskIntoConstraints = false

    parentView.addSubview(hotSpot)
    hotSpot.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    UIView.animateWithDuration(4.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity:1, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            //self.hotSpotOne.center.x += self.view.bounds.width
            hotSpot.alpha = 1.0
            hotSpot.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
            hotSpot.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "showPopover"))
            }, completion: nil)

        let horizontalConstraint = NSLayoutConstraint(item: hotSpot, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: parentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -1)
        let verticalConstraint = NSLayoutConstraint(item: hotSpot
            , attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: parentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 16)
        let widthConstraint = NSLayoutConstraint(item: hotSpot, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50)
        let heightConstraint = NSLayoutConstraint(item: hotSpot, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50)

    parentView.addConstraints([verticalConstraint, horizontalConstraint, widthConstraint, heightConstraint])

}

}

这就是我调用扩展程序的方式:

hotSpotOne.helptip(self.hotSpotOne, parentView: self.view)// first circle

现在我想做的是这样的:

hotSpotTwo.helptip(self.hotSpotTwo, parentView: self.view) //second circle

我的帮助提示类如下所示:

   class HelpTips: UIView {
var selectedColor: UIColor = UIColor.TRLMHelpTipYellowColor(){
    didSet{
        self.setNeedsDisplay()
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func drawRect(rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    CGContextSetLineWidth(context, 1)
    CGContextSetStrokeColorWithColor(context, UIColor.TRLMHelpTipStrokeColor().CGColor)
    let circle = CGRectMake(5, 5, 40, 40)
    CGContextAddEllipseInRect(context, circle)
    CGContextStrokePath(context)
    selectedColor.setFill()
    CGContextFillEllipseInRect(context, circle)
}

 }

我想对上面的圆使用具有不同约束的相同扩展,因为它被放置在 View 中的不同位置。任何帮助表示赞赏。谢谢!

最佳答案

我当前的想法是将如下内容添加到您的 HelpTips 类中:

var constraintList: [NSLayoutConstraint] = []
func helptip(parentView: UIView){
    self.tag = 1
    self.userInteractionEnabled = true
    self.backgroundColor = UIColor.clearColor()
    self.translatesAutoresizingMaskIntoConstraints = false

    parentView.addSubview(self)
    UIView.animateWithDuration(4.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity:1, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
        //self.hotSpotOne.center.x += self.view.bounds.width
        self.alpha = 1.0
        self.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "showPopover"))
        }, completion: nil)
    if constraintList.count > 3 {
        parentView.addConstraints([constraintList[0], constraintList[1], constraintList[2], constraintList[3]])
    } else {
        print("Must initialize constraints before displaying.")
    }
}

调用者将负责提供约束,调用顺序将是:

var hsp1 = HelpTips(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
hsp1.constraintList.append(NSLayoutConstraint(item: hsp1, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: parentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -1))
hsp1.constraintList.append(NSLayoutConstraint(item: hsp1
    , attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: parentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 16))
hsp1.constraintList.append(NSLayoutConstraint(item: hsp1, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
hsp1.constraintList.append(NSLayoutConstraint(item: hsp1, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 50))
hsp1.helptip(parentView)

(更好的方法是在类中添加 addConstraint 方法,这样调用者就不需要访问 constraintList 数组。)

关于ios - 如何创建一个具有创建可放置在不同位置的 UIView 的函数的扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32895653/

相关文章:

ios - 单点触控按钮中的事件处理程序问题

ios - 尝试在 Delphi 中编译 64 位 iOS 设备

ios - 如何从 URL 加载图像并将其显示在 tableView 单元格中 (Swift 3)

arrays - 在数组中存储值与在 Swift 中创建扩展函数

swift - 可以使用 Swift 中的实现者定义的功能扩展协议(protocol)吗?

ios - 从后端服务器提取内容更新的最佳解决方案?

iOS Swift base64encoding 与 PAW 应用程序 Base64 编码不同

iOS 启动 Storyboard : How can I change a Tab Bar's tint color?

ios - Swift 获取字符串中最后一次出现字符后的子字符串

swift - 如何扩展两个导出(swift 2)