Touch event-drag view, touch and drag view

Source: Internet
Author: User

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}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.