美文网首页
View的旋转和放大缩小

View的旋转和放大缩小

作者: Dove_Q | 来源:发表于2016-08-25 09:23 被阅读33次
     var rotation: CGFloat = 0
        var scale: CGFloat = 1
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            //1. 视图默认可以接收触摸事件
            let firstView = FirstView(frame: CGRect(x: 100, y: 100, width: 300, height: 300))
            //当父视图不能交互时,所有子视图都不能交互
            //UIImageView: 默认为false,不能交互
    //        firstView.userInteractionEnabled = false
            firstView.backgroundColor = UIColor.redColor()
            self.view.addSubview(firstView)
            
            //2. 视图结构不能改变事件传递顺序
            let secondView = SecondView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
            //用户是否可以交互
            //UIView默认为true
    //        secondView.userInteractionEnabled = false
            secondView.backgroundColor = UIColor.greenColor()
    //        self.view.addSubview(secondView)
            firstView.addSubview(secondView)
            
            let rotate = UIRotationGestureRecognizer(target: self, action: #selector(didRotate(_:)))
            firstView.addGestureRecognizer(rotate)
            
            let pinch = UIPinchGestureRecognizer(target: self, action: #selector(didPinch(_:)))
            firstView.addGestureRecognizer(pinch)
        }
        //旋转
        func didRotate(sender: UIRotationGestureRecognizer) {
            let firstView = sender.view!
    //        firstView?.transform = CGAffineTransformMakeRotation(sender.rotation)
            
            firstView.transform = CGAffineTransformRotate(firstView.transform, sender.rotation - rotation)
            rotation = sender.rotation
            
            print(sender.rotation)
            
            if sender.state == .Ended {
                rotation = 0
            }
            
            //手势每次都是从0开始
            print("rotate: ", sender.rotation)
        }
        //放大、缩小
        func didPinch(sender: UIPinchGestureRecognizer) {
            let firstView = sender.view!
            
            //??????
            firstView.transform = CGAffineTransformScale(firstView.transform, sender.scale - scale + 1 , sender.scale - scale + 1)
            
            scale = sender.scale
            
            if sender.state == .Ended {
                scale = 1
            }
            
            print("scale: ", sender.scale)
        }
    

    相关文章

      网友评论

          本文标题:View的旋转和放大缩小

          本文链接:https://www.haomeiwen.com/subject/cbhosttx.html