Touch event-drag view, touch and drag view
Case: Drag imageView, view1, and view2 by touch events
Note: 1. imgeview does not respond to touch events by default. 2. view has three subviews. How to differentiate multiple views?
Connect two views with one imageView and one IBOutlet on the storyBoard.
Case image: [upload later]
// Enable imageView to be touched by the user
1 [self.imageView setUserInteractionEnabled:YES];
View to distinguish
1 [touch view] == self.imageView
Touch Event code
1-(void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event 2 {3 // 1. get user click 4 UITouch * touch = [touches anyObject]; 5 CGPoint location = [touch locationInView: self. view]; 6 CGPoint preLocation = [touch previuslocationinview: self. view]; 7 CGPoint dertPoint = CGPointMake (location. x-preLocation. x, location. y-preLocation. y); 8 // 2. judge that the view 9 if ([touch view] = self. imageView) {10 NSLog (@ "Click image"); 11 12 [self. imageView setCenter: CGPointMake (self. imageView. center. x + dertPoint. x, self. imageView. center. y + dertPoint. y)]; 13} else if ([touch view] = self. redView) {14 NSLog (@ "Click hongse"); 15 [self. redView setCenter: CGPointMake (self. redView. center. x + dertPoint. x, self. redView. center. y + dertPoint. y)]; 16 17} else if ([touch view] = self. greenView) {18 // It is recommended that you do not directly use else for processing, because it is possible to add a new control 19 NSLog (@ "Click green") at any time; 20 [self. greenView setCenter: CGPointMake (self. greenView. center. x + dertPoint. x, self. greenView. center. y + dertPoint. y)]; 21} 22 23}