[Swift 2.1] adds click events and click effects for UIView, and swiftuiview

Source: Internet
Author: User

[Swift 2.1] adds click events and click effects for UIView, and swiftuiview

 

Preface

Unlike UIButton, when a click event is added, UIView has a low click effect and a poor user experience. Here, you can customize and expand them to achieve the effect similar to UIButton.

 

Statement
You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com
Farmer's uncle: http://over140.cnblogs.com

 

Body

1. Add click events for UIView

extension UIView {    func addOnClickListener(target: AnyObject, action: Selector) {        let gr = UITapGestureRecognizer(target: target, action: action)        gr.numberOfTapsRequired = 1        userInteractionEnabled = true        addGestureRecognizer(gr)    }}

 

 

2. Add click effects for UIView

class UIViewEffect : UIView {    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {        backgroundColor = UIColor.groupTableViewBackgroundColor()    }    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {        UIView.animateWithDuration(0.15, animations: { () -> Void in            self.backgroundColor = UIColor.clearColor()        })    }    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {        UIView.animateWithDuration(0.15, animations: { () -> Void in            self.backgroundColor = UIColor.clearColor()        })    }}

You can change the click effect here. For UIImageView, you can change the click transparency.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.