如何使用创建可以放置在不同位置的UIView的函数创建扩展?

时间:2015-10-01 19:44:57

标签: ios swift swift-extensions

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

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

我的helptips类看起来像这样:

   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)
}

 }

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

1 个答案:

答案 0 :(得分:0)

我目前的想法是在您的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数组。)