第六周Swift總結(jié)

day One

如果是ViewController就用presen方法進(jìn)行跳轉(zhuǎn)前進(jìn) 用dismiss跳轉(zhuǎn)后退
如果是navigation就用push方法進(jìn)行跳轉(zhuǎn)前進(jìn) 用pop跳轉(zhuǎn)后退

    func buttonAction(){

        self.view.addTransitionAnimation(0.4, type: TransitionType.Fade, direction: TransitionDirection.FromRight)
        
        let second = SecondViewController()
        
        self.presentViewController(second, animated: true, completion: nil)
    }

兩個(gè)視圖控制器的重合和展開(kāi)
這是后面的視圖

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.redColor()
        //1.創(chuàng)建視圖控制器對(duì)象
        let second = SecondViewController()
        //2.將second作為當(dāng)前視圖控制器的子視圖控制器
        // 當(dāng)前視圖控制就可以將second的view屬性當(dāng)成一般的視圖去使用
        self.addChildViewController(second)
        //3.將second的View作為子視圖添加到當(dāng)前界面上
        self.view.addSubview(second.view)
//        //b.設(shè)置second的veiw的frame(默認(rèn)坐標(biāo)是(0胀糜,0)大小是屏幕的大小)
//        second.view.frame = CGRectMake(100, 0, self.view.bounds.width, self.view.bounds.height)
    }

這是前面的視圖

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.blueColor()
        //添加按鈕 點(diǎn)擊按鈕  發(fā)生側(cè)滑
        
        let btn = UIButton(frame: CGRectMake(20,50,100,50))
        
        btn.setTitle("頭像", forState: .Normal)
        
        btn.addTarget(self, action: "btnAction:", forControlEvents: .TouchDown)
        
        self.view.addSubview(btn)
    }

}
extension SecondViewController{
    func btnAction(btn:UIButton){
        if self.view.frame.origin.x == 0{
            UIView.animateWithDuration(0.4){
                self.view.transform = CGAffineTransformMakeScale(0.75, 0.75)
                self.view.center.x += 150
            }
        }else {
            UIView.animateWithDuration(0.4){
            self.view.transform = CGAffineTransformMakeScale(1,1)
                self.view.frame.origin.x = 0

            }

        }
        //點(diǎn)擊頭像側(cè)滑


    }
}

實(shí)現(xiàn)一些隱藏在背后的視圖的一些功能 比較新穎

day two

//用閉包做反向傳值畏铆,就是利用閉包的聲明,實(shí)現(xiàn)和調(diào)用
//1.在下一個(gè)界面中聲明閉包(將要穿的值通過(guò)閉包的參數(shù)來(lái)傳)
//2.在上一個(gè)界面中切換或跳轉(zhuǎn)到下一個(gè)界面的時(shí)候去實(shí)現(xiàn)閉包
//3.在下一個(gè)界面消失的時(shí)候去調(diào)用閉包
前者接收值 前者對(duì)象

        let second = SecondViewController()
        second.sendValue = {(value) in

            //使用從上一個(gè)界面?zhèn)鬟^(guò)來(lái)的值
            self.textField.text = value
        }

后者發(fā)送值 后者對(duì)象

var sendValue:((String) ->Void)? = nil

self.sendValue!(self.textField.text!)

注意晃跺;閉包的實(shí)現(xiàn)都是在button中實(shí)現(xiàn)

//消息中心:相當(dāng)于生活中的廣播站 1.用來(lái)發(fā)送消息2.一個(gè)消息中心可以發(fā)送多條消息 每條消息以不同的消息名來(lái)區(qū)分
//觀察者:相當(dāng)于收音機(jī) 1.用來(lái)接收消息 2.能接受到消息的前提:a.消息中心發(fā)送消息 并且實(shí)時(shí)的 b.觀察者觀察的消息名要和消息中心發(fā)送的消息的消息名保持一致 3. 同一個(gè)觀察者可以接受不同的消息
//消息中心發(fā)送的內(nèi)容/觀察者接收的內(nèi)容

//消息中心做反向傳值衰琐;在下一個(gè)界面中使用消息中心發(fā)送消息(消息內(nèi)容就是要傳的值):在上一個(gè)界面注冊(cè)觀察者來(lái)接收消息

觀察者

    //2.注冊(cè)成為觀察者
    //參數(shù)1:
    //參數(shù)2:消息中心發(fā)送信息的時(shí)候觀察者會(huì)自動(dòng)調(diào)用的方法對(duì)應(yīng)的selector(觀察者接收到消息后會(huì)調(diào)用的方法)-> 必須帶一個(gè)參數(shù) 并且參數(shù)類(lèi)型是NSNOtification
    //參數(shù)3:觀察者要觀察的消息的名字
    override func viewDidLoad() {
        self.title = "界面1"
        super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "notificationAction:", name: "nof1", object: nil)
    }
    func notificationAction(nof:NSNotification){
        //object屬性就是消息的內(nèi)容
        print("接收消息:\(nof.object)")
        self.textField.text = nof.object as? String
    }

消息中心
//1.使用消息中心發(fā)送消息(消息內(nèi)容就是要傳的值)

//a.拿到消息中心(單列對(duì)象)

//b.發(fā)送消息
//參數(shù)1:消息名(相當(dāng)于頻段)

//參數(shù)2:要發(fā)送的消息內(nèi)容

        NSNotificationCenter.defaultCenter().postNotificationName("nof1", object: self.textField.text)

還有其他功能

       //注冊(cè)成為觀察者
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "notificationAction:", name: "nof1", object: nil)
        
        //移除觀察者
        //移除所有的觀察者
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
        //移除指定的觀察者
        NSNotificationCenter.defaultCenter().removeObserver(self, name: "dfd", object: nil)
    }
    
    
    func notificationAction(nof:NSNotification){
        
        self.textField.text = nof.object as? String
    }

單例對(duì)象

三個(gè)部分
接受者

    override func viewWillAppear(animated: Bool) {
        
        //通過(guò)單例對(duì)象拿到值
        self.textField.text = ValueManager.defalutManager.sendValue
    }

發(fā)送者 (zai button中)

 ValueManager.defalutManager.sendValue = self.textField.text!

創(chuàng)建一個(gè)類(lèi)

class ValueManager: NSObject {
    //保證當(dāng)前這個(gè)類(lèi)只能創(chuàng)建一個(gè)對(duì)象   并且這個(gè)對(duì)只能通過(guò)defalutManager去拿到
    //拿到當(dāng)前這個(gè)類(lèi)唯一的對(duì)象
    static let defalutManager = ValueManager()
            //構(gòu)造方法私有化
    private override init() {
    }
    //2.聲明一個(gè)屬性的類(lèi)型是需要傳的值的類(lèi)型
    var sendValue = ""
}

創(chuàng)建導(dǎo)航控制器

        //導(dǎo)航控制器
        //UINavigationController ->UIViewController
        //UIViewcontroller的屬性和方法UINavigationController都擁有
        //特點(diǎn):
        //a也糊。默認(rèn)會(huì)顯示一個(gè)導(dǎo)航條 一個(gè)隱藏的工具條
        //b。系統(tǒng)自帶的資格封裝好的容器視圖控制器   專(zhuān)門(mén)用來(lái)管理其他的視圖控制器 通過(guò)一個(gè)棧結(jié)構(gòu)的容器來(lái)管理 c.導(dǎo)航控制器上永遠(yuǎn)顯示的當(dāng)前棧結(jié)構(gòu)的棧的元素(最上層的視圖控制器對(duì)象)d.可以通過(guò)將視圖控制器設(shè)置為導(dǎo)航控制器的根視圖控制器和通過(guò)導(dǎo)航控制器調(diào)用push方法將視圖控制器添加到導(dǎo)航控制器的棧結(jié)構(gòu)中 e.通過(guò)導(dǎo)航控制器調(diào)用pop方法將棧結(jié)構(gòu)中的視圖控制器移除
       //
        //

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        self.window?.backgroundColor = UIColor.whiteColor()
        
        //創(chuàng)建導(dǎo)航控制器對(duì)象(導(dǎo)航控制器必須要有一個(gè)根視圖控制器)
        
        let nav = UINavigationController(rootViewController: FirstViewController())

        //2.將指定的控制器添加到導(dǎo)航控制器的棧結(jié)構(gòu)哦羡宙、中
        //入棧
        let  second = SecondViewController()

        nav.pushViewController(second, animated: true)

        //3.將導(dǎo)航控制器棧結(jié)構(gòu)中的棧頂元素移除
        //pop  出棧
        nav.popViewControllerAnimated(true)

        //將導(dǎo)航控制器設(shè)置為根視圖控制器
        self.window?.rootViewController = nav
        return true
    }

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        self.window?.backgroundColor = UIColor.whiteColor()
        
        let nav = UINavigationController(rootViewController: OneViewController())
        
        self.window?.rootViewController = nav

導(dǎo)航控制器的視圖與視圖之間的轉(zhuǎn)換

    override func buttonAction() {
        //進(jìn)入下一個(gè)界面
        self.navigationController?.pushViewController(ThreeViewController(), animated: true)
    }

導(dǎo)航控制里面的好狸剃、棧元素移除

    override func buttonAction() {
        //拿到導(dǎo)航控制器
        
        let nav = self.navigationController
        
        
        //1.將當(dāng)前導(dǎo)航控制器對(duì)應(yīng)的棧結(jié)構(gòu)中的棧元素移除
//        nav?.popViewControllerAnimated(true)
        //2.將當(dāng)前導(dǎo)航控制器對(duì)應(yīng)的棧結(jié)構(gòu)中除了根視圖以外其他的視圖控制器對(duì)象全部移除
//        nav?.popToRootViewControllerAnimated(true)
        //3.將導(dǎo)航控制器對(duì)應(yīng)的棧結(jié)構(gòu)中指定的視圖控制器對(duì)象前面的所有的視圖控制器移除
        //拿到當(dāng)前視圖控制器的所有的子視圖控制器(拿到棧結(jié)構(gòu)中的所有對(duì)象)
        let childArray = nav?.childViewControllers

        nav?.popToViewController(
            childArray![0], animated: true)
    }

定制導(dǎo)航條

    //定制導(dǎo)航條
    func navigationBarSetting(){
        
        //1.設(shè)置是否有透明度(默認(rèn)有透明度)
        //true -> 視圖控制器的子視圖的坐標(biāo)原點(diǎn)在(0,0)位置   相對(duì)于手機(jī)屏幕
        //false -> 視圖控制器的子視圖的坐標(biāo)原點(diǎn)在(0辛辨,64)位置  相對(duì)于手機(jī)屏幕
        self.navigationBar.translucent = true
        
        //2.設(shè)置導(dǎo)航條的背景顏色
        
        self.navigationBar.barTintColor = UIColor.orangeColor()
        
        //3.設(shè)置背景圖片(用得少)
        //注意:想要圖片的實(shí)際高度必須大于等于64
        self.navigationBar.setBackgroundImage(UIImage(named: "2.jpg"), forBarMetrics: .Default)
        
        //4.設(shè)置填充顏色
        //設(shè)置鏤空顏色(默認(rèn)是藍(lán)色)
        self.navigationBar.tintColor = UIColor.redColor()
        
        //5.設(shè)置的導(dǎo)航條中間默認(rèn)顯示的文字的屬性
        //NSFontAttributeName -> 字體對(duì)應(yīng)的key
        self.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.systemFontOfSize(20),NSForegroundColorAttributeName:UIColor.whiteColor()]

    }

day three

設(shè)置狀態(tài)欄(電池框框那個(gè)欄)

//將狀態(tài)欄變成白色(默認(rèn)是黑色)
//1.在info.plist文件中去添加一個(gè)鍵值對(duì) -> View controller-based status bar appearance:NO
//2.通過(guò)應(yīng)用程序?qū)ο蟾淖儬顟B(tài)欄的樣式
//a.拿到當(dāng)前應(yīng)用程序?qū)ο?let app = UIApplication.sharedApplication()
//b.設(shè)置狀態(tài)欄的樣式
//LightContent -> 白色
app.statusBarStyle = .LightContent

定制導(dǎo)航條

        //定制導(dǎo)航條
        //1.設(shè)置背景顏色
        self.navigationBar.barTintColor = UIColor.blackColor()
        
        //2.設(shè)置填充顏色
        self.navigationBar.tintColor = UIColor.whiteColor()

創(chuàng)建導(dǎo)航欄上的按鈕 這個(gè)方法最好適用又要圖片和文字的捕捂。

//a.通過(guò)其他視圖來(lái)創(chuàng)建一個(gè)UIBarButtonItem對(duì)象
let btn = UIButton.init(frame: CGRectMake(0, 0, 50, 30))
btn.setTitle("注冊(cè)", forState: .Normal)
btn.setBackgroundImage(UIImage.init(named: "buttonbar_action.png"), forState: .Normal)
btn.setTitleColor(UIColor.greenColor(), forState: .Normal)
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchDown)

let item1 = UIBarButtonItem.init(customView: btn)

只是圖片的情況

//參數(shù)1:圖片
//參數(shù)2:item的風(fēng)格
//參數(shù)3:調(diào)用方法的對(duì)象
//參數(shù)4:當(dāng)當(dāng)前item對(duì)應(yīng)的按鈕被點(diǎn)擊后會(huì)調(diào)用的方法
let item2 = UIBarButtonItem.init(image: UIImage.init(named: "itemImage")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "itemAction:")

只是文字的情況

        let item3 = UIBarButtonItem.init(title: "注冊(cè)", style: .Plain, target: self, action: "itemAction:")

系統(tǒng)樣式來(lái)創(chuàng)建 創(chuàng)建左右兩邊的 可以多個(gè)

let item4 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "itemAction:")


//2.設(shè)置navigationBar右邊的顯示
//a.在右邊顯示一個(gè)視圖
self.navigationItem.rightBarButtonItem = item4
//b.在右邊顯示顯示多個(gè)視圖
//self.navigationItem.rightBarButtonItems = [item4,item3]

//3.設(shè)置navigationBar左邊的顯示
self.navigationItem.leftBarButtonItem = item2
//self.navigationItem.leftBarButtonItems = [item1, item2]

在navigationBar中間顯示

//a.顯示純文字
self.title = "登錄"

//b.通過(guò)一個(gè)label顯示文字
let label = UILabel.init(frame: CGRectMake(0, 0, 100, 30))
label.text = "登錄"
label.textColor = UIColor.yellowColor()
label.textAlignment = .Center

//c.創(chuàng)建中間顯示的分段選中器
let segement = UISegmentedControl.init(items: ["海賊","火影"])
segement.frame = CGRectMake(0, 0, 100, 40)
segement.selectedSegmentIndex = 0


//設(shè)置中間視圖
self.navigationItem.titleView = segement
//設(shè)置背景顏色
self.view.backgroundColor = UIColor.orangeColor()

//設(shè)置rightItem
let item = UIBarButtonItem.init(barButtonSystemItem: .Cancel, target: self, action: "itemAction")
self.navigationItem.rightBarButtonItem = item

//1.替換系統(tǒng)自帶的返回按鈕 -> 設(shè)置leftBarButtonItem屬性
let item2 = UIBarButtonItem.init(barButtonSystemItem: .Done, target: self, action: "itemAction")

//self.navigationItem.leftBarButtonItem = item2

//2.隱藏系統(tǒng)自帶的返回按鈕
self.navigationItem.hidesBackButton = true

工具欄

       //創(chuàng)建toolBar上顯示的按鈕對(duì)應(yīng)的item
        let item1 = UIBarButtonItem.init(barButtonSystemItem: .Camera, target: self, action: "toolBarAction")
        let item2 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "toolBarAction")
        let item3 = UIBarButtonItem.init(barButtonSystemItem: .Edit, target: self, action: "toolBarAction")
        let item4 = UIBarButtonItem.init(barButtonSystemItem: .Refresh, target: self, action: "toolBarAction")
        
        
        //a.FlexibleSpace(相當(dāng)于空格,用來(lái)設(shè)置每個(gè)item之間的間距斗搞,間距是自動(dòng)計(jì)算的)
        let space1 = UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: "")
        //b.FixedSpace(相當(dāng)于空格指攒,用來(lái)設(shè)置每個(gè)item之間的間距,間距需要手動(dòng)設(shè)置)
        let space2 = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: "")
        //設(shè)置間距
        space2.width = 20
        
        
        //將item添加到toolBar上
        self.toolbarItems = [space2,item1,space2,item2,space2,item3,space2,item4,space2]
                
        //1.定制toolBar(高度是44)
        //toolBar屬于導(dǎo)航控制器,默認(rèn)是隱藏的
        //a.讓toolBar顯示出來(lái)
        self.toolbarHidden = false
        
        //b.設(shè)置是否有透明度(默認(rèn)true->有透明度)
        self.toolbar.translucent = false
        
        //c.設(shè)置背景顏色
        self.toolbar.barTintColor = UIColor.yellowColor()
        //d.設(shè)置填充顏色
        self.toolbar.tintColor = UIColor.redColor()
        

        //2.定制導(dǎo)航條
        //a.設(shè)置背景顏色
        self.navigationBar.barTintColor = UIColor.blackColor()
        
        //b.設(shè)置填充顏色
        self.navigationBar.tintColor = UIColor.whiteColor()

day Four

UITabBarController

一般的先創(chuàng)建tabBar標(biāo)簽欄 設(shè)置顏色

        //1.設(shè)置是否透明(默認(rèn)是true)
        
        self.tabBar.translucent = true
        
        
        //2.設(shè)置背景顏色
        self.tabBar.barTintColor = UIColor.redColor()
        
        //3.設(shè)置背景圖片
        
//        self.tabBar.backgroundImage = UIImage(named: "")
        
        //4.設(shè)置填充顏色
        self.tabBar.tintColor = UIColor.yellowColor()

然后通過(guò)標(biāo)簽欄 創(chuàng)建tabBarItem 設(shè)置具體的文字和圖片 文字大小顏色 圖片不被點(diǎn)擊的照片和被點(diǎn)擊時(shí)的照片

    func tabBarItemSetting(){
        
        //拿到tabBar上所有的tabBaritem的對(duì)象
        
        let items = self.tabBar.items
        
        let titles = ["調(diào)慢","繪本","專(zhuān)題","我的"]
        
        
        let imageNames = ["tiaoman","huiben","zhaunti","wode"]
        
        //設(shè)置item
        for (i,item) in items!.enumerate(){
            
            item.title = titles[i]
            
            item.image = UIImage(named: imageNames[i]+"_u")?.imageWithRenderingMode(.AlwaysOriginal)
            //設(shè)置選中狀態(tài)的圖片
            item.selectedImage = UIImage(named: imageNames[i]+"_d")?.imageWithRenderingMode(.AlwaysOriginal)
            //設(shè)置文字的屬性
 item.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(15),NSForegroundColorAttributeName:UIColor.lightGrayColor()], forState: .Normal)
        }

    }

最后是將視圖傳過(guò)來(lái) 作為其子視圖

        let two = TwoViewController()
        two.title = "two"
        let three = ThreeViewController()
        three.title = "three"
        self.viewControllers = [one,two,three]

設(shè)計(jì)到UITabBarController和UINavigationController UIViewController的時(shí)候
將UINavigation弄到UITabBarController里面作為子控件 而UITabBarController是UIWindow的根視圖 最后直接再把UIViewController弄到UINavigation中作為子視圖

    func creatControllers(){
        let one = OneViewController()
        one.title = "one"
        let nav1 = YTNavigationController(rootViewController:one)
        let two = OneViewController()
        two.title = "two"
        let nav2 = YTNavigationController(rootViewController:two)
        let three = OneViewController()
        three.title = "three"
        let nav3 = YTNavigationController(rootViewController:three)
        //將導(dǎo)航控制器對(duì)象作為標(biāo)簽欄控制器的子視圖控制器
        self.viewControllers = [nav1,nav2,nav3]

    }

這里用了一個(gè)沙盒來(lái)判斷誰(shuí)做根視圖


        if NSUserDefaults.standardUserDefaults().boolForKey("isOpenBefore"){
                    //將標(biāo)簽欄控制器作為window的根視圖控制器
            self.window?.rootViewController = YTTabBarController()

        }else{

            //將引導(dǎo)頁(yè)作為根控制器
            self.window?.rootViewController = LeadViewController()
            //設(shè)置本地?cái)?shù)據(jù) 記錄程序已經(jīng)打開(kāi)過(guò)
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isIpenBefore")
        }

手動(dòng)添加進(jìn)去

    }
    func tabBaritemSetting(){
        let one = OneViewController()
        one.title = "我的"
        //設(shè)置tabBarItem
        //a.設(shè)置文字
        one.tabBarItem.title = "調(diào)慢"
        //設(shè)置圖片
        //正常情況下的圖片
        one.tabBarItem.image = UIImage(named: "1")?.imageWithRenderingMode(.AlwaysOriginal)
        //選中狀態(tài)的圖片
        one.tabBarItem.selectedImage = UIImage(named: "1")?.imageWithRenderingMode(.AlwaysOriginal)
        let two = TwoViewController()
        two.title = "其他"
        two.tabBarItem.image = UIImage(named: "2")?.imageWithRenderingMode(.AlwaysOriginal)
        //選中狀態(tài)的圖片
        two.tabBarItem.selectedImage = UIImage(named: "2")?.imageWithRenderingMode(.AlwaysOriginal)
        let three = ThreeViewController()
        three.title = "寫(xiě)作"
        three.tabBarItem.image = UIImage(named: "3")?.imageWithRenderingMode(.AlwaysOriginal)
        //選中狀態(tài)的圖片
        three.tabBarItem.selectedImage = UIImage(named: "3")?.imageWithRenderingMode(.AlwaysOriginal)
        let four = FourViewController()
        four.title = "消息"
        four.tabBarItem.image = UIImage(named: "4")?.imageWithRenderingMode(.AlwaysOriginal)
        //選中狀態(tài)的圖片
        four.tabBarItem.selectedImage = UIImage(named: "4")?.imageWithRenderingMode(.AlwaysOriginal)
        let five = FiveViewController()
        five.title = "我的"
        five.tabBarItem.image = UIImage(named: "5")?.imageWithRenderingMode(.AlwaysOriginal)
        //選中狀態(tài)的圖片
        five.tabBarItem.selectedImage = UIImage(named: "5")?.imageWithRenderingMode(.AlwaysOriginal)
        self.viewControllers = [one,two,three,four,five]
    }

這里通過(guò)一個(gè)touchesBegan來(lái)獲取手勢(shì)點(diǎn)擊的點(diǎn)的位置的坐標(biāo)

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        //參數(shù)1僻焚;touches ->多個(gè)手指觸摸屏幕的時(shí)候只能獲取一個(gè)對(duì)象
        //拿到當(dāng)前的觸摸對(duì)象
        
        let touch = touches.first
        
        //拿到當(dāng)前這個(gè)觸摸點(diǎn)的對(duì)象
        //坐標(biāo)點(diǎn)的相對(duì)視圖
        let location = touch?.locationInView(self.view)
        print(location)
        //參數(shù)2允悦;event ->可以拿到多個(gè)觸摸對(duì)象 
        let allTouches = event?.allTouches()
        print("ALL:\(allTouches?.count)")

        //遍歷拿到每個(gè)觸摸對(duì)象
        for item in allTouches!{
            print(item.locationInView(self.view))

        }
        print("開(kāi)始觸摸")
    }

點(diǎn)擊手勢(shì) 先創(chuàng)建一個(gè)點(diǎn)擊對(duì)象tap病友一個(gè)方法 然后確定點(diǎn)擊次數(shù) 如果是圖片和label需要打開(kāi)用戶(hù)交互才可以進(jìn)行點(diǎn)擊手勢(shì) 最后將對(duì)象添加到視圖上 注意 方法中具體是為了實(shí)現(xiàn)一些功能

        let tap = UITapGestureRecognizer(target: self, action: "tapAction:")
        
        //核心屬性:點(diǎn)擊次數(shù)
        tap.numberOfTapsRequired = 2
    
        //打開(kāi)圖片的用戶(hù)交互 并且添加點(diǎn)擊手勢(shì)
        self.imageView.userInteractionEnabled = true
        
        //將點(diǎn)擊的手勢(shì)添加到視圖上(要求;添加的手勢(shì)的視圖必須可可以進(jìn)行用戶(hù)交互)
        self.view.addGestureRecognizer(tap)

以下手勢(shì)和上面都差不多 三步走 這里不一一說(shuō)明
長(zhǎng)按手勢(shì)
滑動(dòng)翻頁(yè)手勢(shì)
拖動(dòng)位移手勢(shì)
縮放手勢(shì)
旋轉(zhuǎn)手勢(shì)


        let longPress = UILongPressGestureRecognizer(target: self, action: "longPressAction:")
        let swipe = UISwipeGestureRecognizer(target: self, action: "swipeAction:")
        let pan = UIPanGestureRecognizer(target: self, action: "panAction:")
        let pinch = UIPinchGestureRecognizer(target: self, action: "pinchAction:")
        let rotationG = UIRotationGestureRecognizer(target: self, action: "rotationAction:")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市隙弛,隨后出現(xiàn)的幾起案子架馋,更是在濱河造成了極大的恐慌,老刑警劉巖全闷,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叉寂,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡总珠,警方通過(guò)查閱死者的電腦和手機(jī)屏鳍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)局服,“玉大人钓瞭,你說(shuō)我怎么就攤上這事∫迹” “怎么了山涡?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)唆迁。 經(jīng)常有香客問(wèn)我鸭丛,道長(zhǎng),這世上最難降的妖魔是什么媒惕? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任系吩,我火速辦了婚禮,結(jié)果婚禮上妒蔚,老公的妹妹穿的比我還像新娘穿挨。我一直安慰自己,他們只是感情好肴盏,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布科盛。 她就那樣靜靜地躺著,像睡著了一般菜皂。 火紅的嫁衣襯著肌膚如雪贞绵。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天恍飘,我揣著相機(jī)與錄音榨崩,去河邊找鬼。 笑死章母,一個(gè)胖子當(dāng)著我的面吹牛母蛛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播乳怎,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼彩郊,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起秫逝,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤恕出,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后违帆,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體浙巫,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年刷后,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了狈醉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡惠险,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出抒线,到底是詐尸還是另有隱情班巩,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布嘶炭,位于F島的核電站抱慌,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏眨猎。R本人自食惡果不足惜抑进,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望睡陪。 院中可真熱鬧寺渗,春花似錦、人聲如沸兰迫。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)汁果。三九已至涡拘,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間据德,已是汗流浹背鳄乏。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留棘利,地道東北人橱野。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像赡译,于是被迫代替她去往敵國(guó)和親仲吏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容