Swift-click the arrow to rotate

Source: Internet
Author: User

Let arrowimage = Uiimageview(Image: UIImage(named: "Machine_arrow")!. Imagewithrenderingmode(. Alwaystemplate))

let Arrowbutton = UIButton(frame: cgrectmake, arrowimage. Bounds. Width, arrowimage. Bounds. Height))

Arrowbutton. addsubview(arrowimage)

Arrowbutton. addTarget(self, action: #selector(btnclicked(_: Event:)), forControlEvents:. touchupinside)

Check the position of the user when the button is clicked and forward the event to the corresponding accessorytapped event

func btnclicked (sender: UIButton, event: anyobject) {

let touches = Event.alltouches ()! As nsset

let touch = touches. Anyobject() as! uitouch

let currenttouchposition = touch. Locationinview(selecttable)

let indexpath: nsindexpath = selecttable. Indexpathforrowatpoint(currenttouchposition)!

TableView(selecttable, Accessorybuttontappedforrowwithindexpath:indexpath)

}

// This way, the Accessorybuttontappedforrowwithindexpath method of UITableView is triggered and a indexpath is obtained parameters. With this indexpath parameter, we can distinguish between which of the many buttons the attachment button has a touch event:

func TableView (tableView: uitableview, Accessorybuttontappedforrowwithindexpath Indexpath: Nsindexpath) {

let idx = Indexpath. Row

// Add your own logic here

print(idx)

// Click the arrow to rotate

rotat()

        //  rotatearrow()

}

// rotate animation one

func Rotat () {

// Create a rotation animation

let anim = cabasicanimation(keypath: "transform.rotation")

// rotation angle

Anim.tovalue = 1 * m_pi

// rotate the time required to specify the angle

Anim.Duration = 1

// rotation Repeat Count

Anim.repeatcount = maxfloat

// animation does not remove after execution

Anim.removedoncompletion = true

// add animations to the Laye of the view

arrowimage. Layer. Addanimation(Anim, Forkey: nil)

// Cancel animation

arrowimage. Layer. Removeallanimations()

// This is an animation of the direction of rotation

UIView. Animatewithduration(0.2) {(), Void in

// Specify a rotation angle of 180°

self. Arrowimage. Transform = cgaffinetransformrotate(self. Arrowimage. Transform, cgfloat(m_pi))

}

}

rotate an animation two

func Rotatearrow () {

UIView. Animatewithduration(0.3, animations: {[weak self] ()--( ) in

if let selfie = self {

Selfie. arrowimage. Transform = cgaffinetransformrotate(selfie. Arrowimage. Transform, cgfloat(m_pi/ +))

}

})

}

Swift-click the arrow to rotate

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.