Developing iOS 8 Apps with Swift Lesson 11学习

来源:互联网 发布:三国如龙传网络不给力 编辑:程序博客网 时间:2024/05/21 09:44

Lesson11:Unwind Segues,Alerts,Timers,View Animation

1、UIAlertController简介

UIAlertController可以参考我之前的一篇文章iOS项目开发实战(Swift)—Button和Alert学习

UIAlertController有Alert和ActionSheet两种样式,在iOS系统中,Alert样式是直接弹窗到屏幕中间,起到一个提示警告的作用。而ActionSheet是从屏幕底部弹出来的,给用户提供更多的选项功能。

下面通过做一个Alert样式的Demo来看看如何用UIAlertController。


Demo描述:点击导航栏右上角的+号,弹出Alert框,同时Alert窗中有文本输入框,通过点击Login,可以获得文本框的值并且在控制台打印出来。


关键代码如下:

 func loadAlert(){        let alertController = UIAlertController(title: "Alert", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)        //登录action        let loginAciton = UIAlertAction(title: "Login", style: .Default) { (action:UIAlertAction) -> Void in            guard let text = alertController.textFields?.first?.text                else{                    return            }            //输出输入文本框的内容            print(text)        }        alertController.addAction(loginAciton)        //取消action        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)        alertController.addAction(cancelAction)        //给Alert添加文本输入框        alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) -> Void in        }                presentViewController(alertController, animated: true, completion: nil)    }

PS:iPad上书写ActionSheet样式和iPhone上有点不同,ipad上面ActionSheet的UIAlertController是从右上角弹出来的,并且悬浮。在书写的时候,需要多添几行代码。而iPhone和iPad上面的Alert样式编写代码的时候是一致。


2、NSTimer

NSTimer定时器主要用到的技术是runloop,在主线程之中使用。经常会用到NSTimer,比如说在做循环滚动新闻的时候,需要让它每隔一小段时间自动执行滚动操作。

NSTimer使用示例如下:

 override func viewDidLoad() {        super.viewDidLoad()        //这样采用Alert样式,actionsheet同理        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "loadAlert")        /**        定时器        - parameter <Tti:     多长时间间隔执行一次        - parameter target:   执行的对象        - parameter selector: 方法        - parameter userInfo: 用户信息        - parameter repeats:  是否重复执行        */        NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "fire:", userInfo: "zengchao", repeats: true)    }        //tolerance:容忍误差,让系统选择一个更高效的时间来执行    func fire(time:NSTimer){        time.tolerance = 0.3        print(time.userInfo as! String)    }

NSTimer的tolerant属性是容忍误差,让系统选择一个更高效的时间来执行。


3、Animation

1)UIView 视图动画

UIView视图动画其实就是对Core Animation进行封装,使用起来很简单。只需要设置好动画之前和动画之后的属性,然后动画是由系统生成的,称为补间动画。一般的动画用UIView Animation就够了,只是自由度会降低。UIView的基础动画中,使用的变化属性通常有位置和大小如bounds/frame/center,外观如backgroundColor/alpha,转换transform如translation/rotation/scale。


下面看几个UIView的视图动画示例:

A:transform属性的类型为CGAffineTransform,其中包括:rotation旋转和scale缩放

案例:

 let rotation = CGAffineTransformMakeRotation(CGFloat(M_PI))        let scale = CGAffineTransformMakeScale(0.5, 0.5)//缩放        UIView.animateWithDuration(1, delay: 0.5, options: [UIViewAnimationOptions.Repeat,UIViewAnimationOptions.Autoreverse ], animations: { () -> Void in            self.button.frame = CGRectMake(0, self.gHeight / 2, self.gWidth / 2, 50)            self.button.backgroundColor = UIColor(red: 23/255, green: 233/255, blue: 233/255, alpha: 1)            self.button.transform = scale            }, completion: nil)

B、Spring Animation 弹性动画

  UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.Repeat, animations: { () -> Void in            }, completion: nil)

C、Transition Animation 转场动画

UIView.transitionFromView(UIView, toView: UIView, duration: NSTimeInterval, options: UIViewAnimationOptions, completion: nil)

D、Keyframe Animation 关键帧动画

UIView.animateKeyframesWithDuration(NSTimeInterval, delay: NSTimeInterval, options: UIViewKeyframeAnimationOptions, animations: { () -> Void in            UIView.addKeyframeWithRelativeStartTime(Double, relativeDuration: Double, animations: { () -> Void in                //code            })            }, completion: nil)

E、贝塞尔曲线运动


2)ViewController转场动画

enum UIModalTransitionStyle: Int {    case CoverVertical   // 底部滑入,默认    case FlipHorizontal  // 水平翻转    case CrossDissolve   // 隐出隐现    case PartialCurl     // 翻页}

3)Core Animation

Core Animation核心动画也包括基本动画、转场动画、关键帧动画、组动画、弹性动画。

CAAnimation实现了CAMediaTiming协议(三个子类)如下:

CAPropertyAnimation:CABasicAnimation/CAKeyFrameAnimation

CAAnimationGroup

CATransition(


4)Dynamic Animation(Lesson12内容)






0 0