博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift - 点击箭头旋转
阅读量:4315 次
发布时间:2019-06-06

本文共 1902 字,大约阅读时间需要 6 分钟。

 

 

let arrowImage = UIImageView(image: UIImage(named: "Machine_arrow")!.imageWithRenderingMode(.AlwaysTemplate))

        let arrowButton = UIButton(frame: CGRectMake(700, 20, arrowImage.bounds.width, arrowImage.bounds.height))

        arrowButton.addSubview(arrowImage)

        arrowButton.addTarget(self, action: #selector(btnClicked(_:event:)), forControlEvents: .TouchUpInside)

 

// 检查用户点击按钮时的位置,并转发事件到对应的accessorytapped事件

    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)

    }

    

    // 这样,UITableViewaccessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath 参数。通过这个indexPath 参数,我们可以区分到底是众多按钮中的哪一个附件按钮发生了触摸事件:

    func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {

        let idx = indexPath.row

        //在这里加入自己的逻辑

        print(idx)

        // 点击箭头旋转

        rotat()

        // rotateArrow()

    }

    

    // 旋转动画一

    func rotat() {

        //创建旋转动画

        let anim = CABasicAnimation(keyPath: "transform.rotation")

        //旋转角度

        anim.toValue = 1 * M_PI

        //旋转指定角度需要的时间

        anim.duration = 1

        //旋转重复次数

        anim.repeatCount = MAXFLOAT

        //动画执行完后不移除

        anim.removedOnCompletion = true

        //将动画添加到视图的laye

        arrowImage.layer.addAnimation(anim, forKey: nil)

        //取消动画

        arrowImage.layer.removeAllAnimations()

        //这个是旋转方向的动画

        UIView.animateWithDuration(0.2) { () -> Void in

            //指定旋转角度是180°

            self.arrowImage.transform = CGAffineTransformRotate(self.arrowImage.transform, CGFloat(M_PI))

        }

    }

 

    // 旋转动画二

    func rotateArrow() {

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

            if let selfie = self {

                selfie.arrowImage.transform = CGAffineTransformRotate(selfie.arrowImage.transform, 180 * CGFloat(M_PI/180))

            }

            })

    }

转载于:https://www.cnblogs.com/gongyuhonglou/p/10311538.html

你可能感兴趣的文章
继承的特点与注意事项
查看>>
C02面向对象
查看>>
Thunder团队第二周 - Scrum会议2
查看>>
转 sql删除重复记录
查看>>
Yum数据库错误
查看>>
HDOJ树形DP专题之考研路茫茫——空调教室
查看>>
《结对-蓝牙考勤系统-测试过程》
查看>>
PAT 1034. Head of a Gang
查看>>
微信分享
查看>>
《数据结构》第1章:绪论
查看>>
基于域名的虚拟主机(最常用)
查看>>
第八讲 shiro 整合 ssm
查看>>
Lucene
查看>>
[LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
查看>>
CNN反卷积理解
查看>>
chrome 中firstChild老是出错
查看>>
Java 7 新的 try-with-resources 语句,自动资源释放
查看>>
封装一个函数, 查看数字在数组中是否出现过, 如果出现过就返回数字在数组中的位置,没有出现过返回-1;...
查看>>
查看用户登录情况
查看>>
双栈排序-NOIP2008T4
查看>>