Swift中UIAlertController的使用

Home / iOS MrLee 2016-2-16 2618

如果你在iOS9.0中还使用UIAlertView的话,那么XCode一定会告诉你你使用的已经过时了,请使用全新的UIAlertController,用它来实现新的提示。那么具体怎么玩呢?我刚好写了一个简单的例子。看源码:
//
//  ViewController.swift
//  SwipeGensture
//
//  Created by leehom on 16/2/1.
//  Copyright © 2016年 lee.demo. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
    
    @IBOutlet var view01: UIView!
    @IBOutlet var testButton: UIButton!
    var offsetX: CGFloat = 0
    var offsetY: CGFloat = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let gestureLeft = UISwipeGestureRecognizer(target: self, action: "handSwipe:")
        gestureLeft.direction = UISwipeGestureRecognizerDirection.Left
        view01.addGestureRecognizer(gestureLeft)
        let gestureRight = UISwipeGestureRecognizer(target: self,action: "handSwipe:")
        gestureRight.direction = UISwipeGestureRecognizerDirection.Right
        view01.addGestureRecognizer(gestureRight)
        let gestureUp = UISwipeGestureRecognizer(target: self, action: "handSwipe:")
        gestureUp.direction = UISwipeGestureRecognizerDirection.Up
        view01.addGestureRecognizer(gestureUp)
        let gestureDown = UISwipeGestureRecognizer(target: self, action: "handSwipe:")
        gestureDown.direction = UISwipeGestureRecognizerDirection.Down
        view01.addGestureRecognizer(gestureDown)
    }
    
    func handSwipe(gesture:UISwipeGestureRecognizer){
        let dir = gesture.direction
        switch(dir){
        case UISwipeGestureRecognizerDirection.Left:
                offsetX -= 20
            break;
        case UISwipeGestureRecognizerDirection.Right:
            offsetX += 20
                break;
        case UISwipeGestureRecognizerDirection.Up:
            offsetY -= 20
            break;
        case UISwipeGestureRecognizerDirection.Down:
            offsetY += 20
            break;
        default:
                break;
        }
        view01.transform = CGAffineTransformMakeTranslation(offsetX,offsetY)
        let x = view01.frame.origin.x
        let y = view01.frame.origin.y
        NSLog("%2f,%2f", x,y)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func btnTestClick(sender: UIButton) {
        if (sender == testButton){
            let alert = UIAlertController(title: "提示", message: "你要退出吗?", preferredStyle: UIAlertControllerStyle.Alert)
            let okAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
                NSLog("YES")
            })
            let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) -> Void in
                NSLog("NO")
            })
            alert.addAction(okAction)
            alert.addAction(cancelAction)
            self.presentViewController(alert, animated: true, completion: { () -> Void in
                NSLog("Done")
            })
        }else{
            NSLog("No")
        }
    }
}

顺带了一个手势的

本文链接:https://www.it72.com/7836.htm

推荐阅读
最新回复 (0)
返回