UIEvent详解:远程控制,运动控制和触摸事件

UIEvent对象代表一个事件。在iOS中,主要有三种事件:触摸事件,运动事件,远程控制事件。远程控制事件主要是外部辅助设备或者耳机的远程命令,例如控制音乐声音的大小,或者下一首歌。运动事件主要是晃动设备等。

触摸事件包括一个或者多个触摸(touches), 每个触摸有一个UITouch对象表示。当触摸事件发生时,系统会通过触摸处理的逻辑找到合适的responder并把UIEvent对象传递过去。responder通过touchesBegan:withEvent:等方法去接收UIEvent对象。

UIResponser都能接收事件,系统提供了几个方法接收UIEvent

    //触摸
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    }
    
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    }
    //摇晃相关
    override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent) {
    }
    
    override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
    }
    
    override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent) {
    }
    
    //远程控制相关
    override func remoteControlReceivedWithEvent(event: UIEvent) {
    }

获得事件的触摸

//所有的触摸
let allTouches = event.allTouches()
//获得UIView的触摸
event.touchesForView(self.view)
//获得UIWindow的触摸
event.touchesForWindow(self.view.window!)
事件的时间戳

//事件的时间戳
event.timestamp

事件中特定手势的触摸

let gesture = UITapGestureRecognizer(target: self, action: "Tap")
event.touchesForGestureRecognizer(gesture)

三种事件类型

UIEventTypeTouches
UIEventTypeMotion
UIEventTypeRemoteControl

事件亚类型

UIEventSubtypeNone                              = 0,//触摸事件的亚类型
        
UIEventSubtypeMotionShake                       = 1,//摇晃
        
UIEventSubtypeRemoteControlPlay                 = 100,//播放
UIEventSubtypeRemoteControlPause                = 101,//暂停
UIEventSubtypeRemoteControlStop                 = 102,//停止
UIEventSubtypeRemoteControlTogglePlayPause      = 103,//播放和暂停切换
UIEventSubtypeRemoteControlNextTrack            = 104,//下一首
UIEventSubtypeRemoteControlPreviousTrack        = 105,//上一首
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,//开始后退
UIEventSubtypeRemoteControlEndSeekingBackward   = 107,//结束后退
UIEventSubtypeRemoteControlBeginSeekingForward  = 108,//开始快进
UIEventSubtypeRemoteControlEndSeekingForward    = 109,//结束快进


猜你喜欢

转载自blog.csdn.net/lcl130/article/details/42141825