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

Ios 如何使用创建UIView的函数创建扩展,该UIView可以放置在不同的位置?,ios,swift,swift-extensions,Ios,Swift,Swift Extensions,我必须创建一个自定义UIView,它实际上是一个具有相同大小和外观的圆,放置在视图中的不同位置。我创建了一个扩展,它可以很好地用于一个圆,但我想使用相同的函数将圆放置在不同的位置。这是我的分机: extension UIView{ func helptip(hotSpot: HelpTips, parentView: UIView){ hotSpot.tag = 1 hotSpot.userInteractionEnabled = true hotSpot.backgr

我必须创建一个自定义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
我这样称呼分机:

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

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

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

 }

我想对上面的圆使用相同的延伸,但约束不同,因为它在视图中放置在不同的位置。感谢您的帮助。谢谢

我目前的想法是在您的
帮助提示
类中添加以下内容:

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
数组。)

如果这样做,现在会出现什么问题?(也就是说,你想在第二次调用时更改哪些值?@PhillipMills我的问题在扩展中我需要做一个检查,比如如果热点==热点{//添加以下约束}否则如果热点==热点{//添加以下约束}否则{//添加以下约束}您可以让每个热点都有自己的约束(或者将它们打包到不同的对象中),以便扩展可以询问传入的对象应该使用什么?是的,我希望这样做,但不知道如何做,我希望每个热点都有自己的约束。这就是为什么我要执行一个检查,然后在扩展中添加约束。我对swift有点陌生。我想我分析这个问题的部分原因是我不明白为什么它是一个
UIView
扩展,而不是
HelpTips
类中的函数。@philipMills好吧,这是我最初的想法,但对我来说似乎并不有效。如果假设屏幕上有5个不同位置的圆圈,那么每次我都必须为每个圆圈添加约束。请参见,每个圆的宽度和高度约束相同,只有垂直和水平约束不断变化。好的,因此在对象内部设置常量约束,并且仅从调用方对更改的约束进行更新。不管怎样……我真正的观点是,对于只作用于定义良好的对象而不是任意的
UIView
的函数来说,
UIView
扩展并不是真正必要的。