簡體   English   中英

UIView擴展內的UIButton

[英]UIButton inside UIView extension

我想知道如何擴展UIView並在擴展函數中處理UIButton的目標選擇器,但我做得不好。 它只是不響應目標選擇器。 我什至嘗試使用代理人披露。 這是代碼。

擴展UIView {

class ClosureDispatch {

    let action: () -> ()

    init(f:() -> ()) {
        self.action = f
    }

    func execute() -> () {
        action()
    }

}


func showMessageView(inView: UIView) {


    var isShowing = false;

    let height: CGFloat = 80.0;
    let paddingBottom: CGFloat = 20.0;


    var backgroundView: UIView  = UIView(frame: CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height))
    backgroundView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.8);

    let close: ClosureDispatch = ClosureDispatch(f: {

        NSLog("go!")

        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

            backgroundView.frame = CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height)

            }) { (completed) -> Void in

                isShowing = false;

        }
    })


    var closeButton: UIButton = UIButton(frame: CGRectMake(inView.frame.size.width-height-paddingBottom, 0, height, height))
    closeButton.setTitle("Close", forState: .Normal)
    closeButton.addTarget(close, action: "execute", forControlEvents: .TouchUpInside)
    closeButton.backgroundColor = UIColor.blueColor()

    closeButton.userInteractionEnabled = true;
    backgroundView.userInteractionEnabled = true;

    backgroundView.addSubview(closeButton)
    inView.addSubview(backgroundView)

    UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

        backgroundView.frame = CGRectMake(0, inView.frame.size.height-height-paddingBottom, inView.frame.size.width, height)

    }) { (completed) -> Void in

        isShowing = true;

        UIView.animateWithDuration(0.5, delay: 3.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

            backgroundView.frame = CGRectMake(0, inView.frame.size.height, inView.frame.size.width, height)

            }) { (completed) -> Void in

                isShowing = false;
        }
    }


}

}

嘗試在execute方法前面添加@objc ,即@objc func execute()...

請參閱此鏈接

如果您的Swift類是從Objective-C類繼承的,則該類中的所有方法和屬性都可用作Objective-C選擇器。 否則,如果您的Swift類不繼承自Objective-C類,則需要在要用作選擇器的符號前添加@objc屬性

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM