ios開発——実用技術編Swift編&マルチタッチとジェスチャー認識

28415 ワード

マルチタッチとジェスチャー認識
 
  1         

  2         //    

  3         var atap = UITapGestureRecognizer(target: self, action: "tapDo:")

  4         self.view.addGestureRecognizer(atap)

  5         atap.numberOfTapsRequired = 1 //    

  6         atap.numberOfTouchesRequired = 1 //    

  7         

  8         //    

  9         var aPan = UIPanGestureRecognizer(target: self, action: "handlenPan:")

 10         self.view.addGestureRecognizer(aPan)

 11         aPan.minimumNumberOfTouches = 1 //      

 12         aPan.maximumNumberOfTouches = 3 //      

 13         

 14         //    

 15         var aLongPress = UILongPressGestureRecognizer(target: self, action: "longPress:")

 16         self.view.addGestureRecognizer(aLongPress)

 17         aLongPress.minimumPressDuration = 1 //       ,  0.5s

 18 

 19         //    

 20         var aPinch = UIPinchGestureRecognizer(target: self, action: "pinchDo:")

 21         self.view.addGestureRecognizer(aPinch)

 22         

 23         //    

 24         var aRotation = UIRotationGestureRecognizer(target: self, action: "rotatePiece:")

 25         self.view.addGestureRecognizer(aRotation)

 26         

 27         //    --   

 28         var leftSwipe = UISwipeGestureRecognizer(target: self, action: "leftSwipe:")

 29         self.view.addGestureRecognizer(leftSwipe)

 30         leftSwipe.direction =  UISwipeGestureRecognizerDirection.Left

 31         

 32         //    --   

 33         var rightSwipe = UISwipeGestureRecognizer(target: self, action: "rightSwipe:")

 34         self.view.addGestureRecognizer(rightSwipe)

 35         rightSwipe.direction =  UISwipeGestureRecognizerDirection.Right

 36         

 37         //    --   

 38         var upSwipe = UISwipeGestureRecognizer(target: self, action: "upSwipe:")

 39         self.view.addGestureRecognizer(upSwipe)

 40         upSwipe.direction =  UISwipeGestureRecognizerDirection.Up

 41         

 42         //    --   

 43         var downSwipe = UISwipeGestureRecognizer(target: self, action: "downSwipe:")

 44         self.view.addGestureRecognizer(downSwipe)

 45         downSwipe.direction =  UISwipeGestureRecognizerDirection.Down

 46     }

 47 

 48     override func didReceiveMemoryWarning() {

 49         super.didReceiveMemoryWarning()

 50         // Dispose of any resources that can be recreated.

 51     }

 52     

 53 

 54     /*

 55     // MARK: - Navigation

 56 

 57     // In a storyboard-based application, you will often want to do a little preparation before navigation

 58     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {

 59         // Get the new view controller using segue.destinationViewController.

 60         // Pass the selected object to the new view controller.

 61     }

 62     */

 63 

 64     

 65     //    

 66     

 67     //         

 68     

 69         

 70 //    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

 71         

 72         

 73         //2015 5 2   ,  :touches --》(touches as NSSet)

 74     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

 75         println("touchesBegan")

 76         

 77         //  touches  

 78         let numTouches = touches.count

 79         

 80         //         

 81         let tapTouches = (touches as NSSet).anyObject()?.tapCount

 82         

 83         //        

 84         let timestamp = event.timestamp

 85         

 86         //       self.view   

 87         let locationPoint = (touches as NSSet).anyObject()?.locationInView(self.view)

 88         

 89         //        self.view   

 90         let previousPoint = (touches as NSSet).anyObject()?.previousLocationInView(self.view)

 91         

 92         //      

 93         self.view.userInteractionEnabled = true

 94         

 95         //      

 96         self.view.multipleTouchEnabled = true

 97         

 98         println("\(tapTouches)")

 99         

100         

101         //          

102         if touches.count == 2

103         {

104             //      

105             let twoTouches = (touches as NSSet).allObjects

106             

107             //      

108             let first:UITouch = twoTouches[0] as! UITouch // 1    

109             let second:UITouch = twoTouches[1]as! UITouch // 2    

110             

111             //   1     self.view   

112             let firstPoint:CGPoint = first.locationInView(self.view)

113             

114             //   1     self.view   

115             let secondPoint:CGPoint = second.locationInView(self.view)

116             

117             //         

118             let deltaX = secondPoint.x - firstPoint.x;

119             let deltaY = secondPoint.y - firstPoint.y;

120             let initialDistance = sqrt(deltaX*deltaX + deltaY*deltaY )

121             

122             println("      :\(initialDistance)")

123         }

124     }

125     

126     //     

127 //    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

128     

129     //2015 5 2   

130     override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

131         

132         println("touchesMoved")

133     }

134     

135     //    

136 //    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

137     

138     //2015 5 2   

139     override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

140         

141         println("touchesEnded")

142     }

143     

144     //      

145     //     :       ,   command+shift+h        home ,    ,  touchesCancelled  ,    、        

146 //    override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {

147     

148     //2015 5 2   

149     override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {

150         

151         println("touchesCancelled")

152     }

153     

154     

155     

156     

157     

158     

159     //  

160     

161     //    

162     func tapDo(sender:UITapGestureRecognizer)

163     {

164         

165         println("    ")

166     }

167     

168     //    

169     func handlenPan(sender:UIPanGestureRecognizer)

170     {

171         println("    ")

172         

173         if sender.state == .Began

174         {

175             //    

176         }

177         else if sender.state == .Changed

178         {

179             //    

180         }

181         else if sender.state == .Ended

182         {

183             //    

184         }

185     }

186     

187     //    

188     func longPress(sender:UILongPressGestureRecognizer)

189     {

190         println("    ")

191         

192         

193     }

194     

195     //    

196     func pinchDo(sender:UIPinchGestureRecognizer)

197     {

198         println("  ")

199     }

200 

201     //    

202     func rotatePiece(sender:UIRotationGestureRecognizer)

203     {

204         println("  ")

205     }

206 

207     

208     //    --   

209     func leftSwipe(sender:UISwipeGestureRecognizer)

210     {

211         println("   ")

212     }

213     

214     //    --   

215     func rightSwipe(sender:UISwipeGestureRecognizer)

216     {

217         println("   ")

218     }

219     

220     //    --   

221     func upSwipe(sender:UISwipeGestureRecognizer)

222     {

223         println("   ")

224     }

225     

226     //    --   

227     func downSwipe(sender:UISwipeGestureRecognizer)

228     {

229         println("   ")

230     }

231