swift ios转场动画详解

来源:互联网 发布:linux nano vim 编辑:程序博客网 时间:2024/05/20 02:22

1.前言

转载请注明出处
http://blog.csdn.net/wangguoyang429883793/article/details/50385423
不得不惊叹ios的动画封装!

2.转场动画

这里写图片描述这里写图片描述

1.比较简单,直接上代码了。最后会有demo。

let cnt = 3  //图片数量    var currentIndex = 0    var img:UIImageView?    var imgs:[UIImage] = [] //要展示的图片容器    var transitionType = "rippleEffect"    private(set) var isNext: Bool {        get{            return true        }        set {            layerTransitionAnimation(newValue) //启动动画        }    }    override func viewDidLoad() {        addImageView()        addOptionsBtn()    }    func addImageView(){        //定义图片控件        img = UIImageView(frame: self.view.bounds)        img?.contentMode = .ScaleAspectFit        img?.image = UIImage(named: "tangwei")        self.view.addSubview(img!)        //添加手势        let leftSwipGesture = UISwipeGestureRecognizer(target: self, action: "onLeftSwipe:")        leftSwipGesture.direction = .Left        self.view.addGestureRecognizer(leftSwipGesture)        let rightSwipeGesture = UISwipeGestureRecognizer(target: self, action: "onRightSwipe:")        rightSwipeGesture.direction = .Right        self.view.addGestureRecognizer(rightSwipeGesture)        imgs.append(UIImage(named: "tangwei")!)        imgs.append(UIImage(named: "gaoyuanyuan1")!)        imgs.append(UIImage(named: "gaoyuanyuan2")!)    }    func onLeftSwipe(gesture:UISwipeGestureRecognizer){        isNext = true    }    func onRightSwipe(gesture:UISwipeGestureRecognizer){        isNext = false    }    //转场动画    func layerTransitionAnimation(isnext:Bool){        //1.创建转场动画对象        let transition = CATransition()        //水波效果        transition.type = transitionType        //设置子类型        if (isnext) {            transition.subtype = kCATransitionFromRight        }else{            transition.subtype = kCATransitionFromLeft        }        transition.duration = 1.0        img?.layer.addAnimation(transition, forKey: "")        img?.image = getImage(isnext)    }    // 获取当前应该展示的图片    func getImage(isnext:Bool)->UIImage{        if (isnext) {            currentIndex = (currentIndex+1)%cnt        }else{            currentIndex = (currentIndex-1+cnt)%cnt        }        return imgs[currentIndex]    }    func addOptionsBtn(){        let btn = UIButton(frame: CGRectMake(0,0,100,60))        btn.setTitle("动画效果", forState: .Normal)        btn.setTitleColor(UIColor.redColor(), forState: .Normal)        btn.addTarget(self, action: "onClick", forControlEvents: .TouchUpInside)        self.view.addSubview(btn)        btn.center = CGPoint(x: UIScreen.mainScreen().bounds.width - 60 , y: 64)    }    func onClick(){        showOptions()    }    //其他效果选项    func showOptions(){        let actionSheet = UIActionSheet()        actionSheet.addButtonWithTitle("骰子效果")        actionSheet.addButtonWithTitle("翻转效果")        actionSheet.addButtonWithTitle("收缩效果")        actionSheet.addButtonWithTitle("向上翻页效果")        actionSheet.addButtonWithTitle("向下翻页效果")        actionSheet.addButtonWithTitle("摄像头打开效果")        actionSheet.addButtonWithTitle("摄像头关闭效果")        actionSheet.addButtonWithTitle("水波效果")        actionSheet.delegate = self        actionSheet.showInView(self.view)    }    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {        switch(buttonIndex){        case 0:            transitionType = "cube"        case 1:            transitionType = "oglFlip"        case 2:            transitionType = "suckEffect"        case 3:            transitionType = "pageCurl"        case 4:            transitionType = "pageUnCurl"        case 5:            transitionType = "cameralIrisHollowOpen"        case 6:            transitionType = "cameraIrisHollowClose"        case 7:            transitionType = "rippleEffect"        default:            break        }    }

其实UIView.transitionWithView 也可以,但是没有这么多得效果。
demo:http://download.csdn.net/detail/wangguoyang429883793/9373376

3.总结

参考地址http://www.cnblogs.com/kenshincui/p/3972100.html
欢迎批评,吐槽,点赞,留言。

1 0
原创粉丝点击