UIPanGestureRecognizer usage Summary

Source: Internet
Author: User

UIPanGestureRecognizer is an extension class of the UIGestureRecognizer class. Its extension classes include runtime, UIPanGestureRecognizer, and UILongPressGestureRecognizer. By using these classes, you can perform operations on a UIView object, such as zoom in, zoom in, move, rotate, slide, and tap. You no longer need to override methods such as touchBegin of UIView to implement these functions. Knowledge Point: UIGestureRecognizer is an abstract class that defines basic gestures. The specific gestures include: 1. Attach UITapGestureRecognizer (any number of hits) 2. Push the cursor inside or outside (used for scaling) 3. Shake or drag UIPanGestureRecognizer (drag) 4. Wipe the cursor (in any direction) 5. Rotate UIRotationGestureRecognizer (move your finger in the opposite direction) 6. Long-pressed UILongPressGestureRecognizer (long-pressed) operations aim to modify the frame, center, bounds attribute of the UIView object and a Transform attribute. I wrote an example to add UIPanGestureRecognizer to UIView and UITableView respectively to achieve the effect of dragging two objects on the screen by holding them by fingers. Declare a UIPanGestureRecognizer object and add it to the UIView object. The UIView class uses this method to dynamically add and delete UIPanGestureRecognizer objects. Using * panRecognizer = [[using alloc] initWithTarget: self action: @ selector (handlePan :)]; [testPanView addGestureRecognizer: panRecognizer]; UIView manages gesture identifiers using the following methods: -removeGestureRecognizer: gestureRecognizers property-gestureRecognizerShouldBegin: In viewDidAppear: method, add views and gesture identifiers dynamically. Then, the two methods to be operated by the reader are used to move the view object. In the two methods, the final method is the CGPoint translatedPoint = [recognizer translationInView: self. view]. The status of each drag operation gets the translatedPoint from the start to the end. It is an absolute value. You can see the difference between the start and end points of the center movement of the dragged view object in the coordinate system corresponding to "self. view. The simplest process is as follows: CGPoint translatedPoint = [recognizer translationInView: self. view]; CGFloat x = recognizer. view. center. x + translatedPoint. x; CGFloat y = recognizer. view. center. y + translatedPoint. y; recognizer. view. center = CGPointMake (x, y); [recognizer setTranslation: CGPointMake (0, 0) inView: self. view]; first, get the value of the Moving Point, then calculate the center value of the view, and add it to the self. in the view coordinate system, the view should be moved to that center and cleared once the view ends. Because the drag operation continues, this process will continue. The processing process of a slightly complex vertex will capture the value of translatedPoint in several states, such as starting, moving, and ending the drag. Then, perform logic processing. For example, the view cannot overflow in the coordinate system of self. view. If it ends, it will automatically slide to a certain position based on the direction. You can find the implementation code of these logic in handlePan2: method. -(Void) viewDidAppear :( BOOL) animated {NSLog (@ "viewDidAppear is at % @. ", [NSDate date]); UIImage * image = [UIImage imageNamed: @" 5.jpg"]; testPanView = [[UIView alloc] initWithFrame: CGRectMake (18, 11,100,100)]; UIImageView * imageview = [[UIImageView alloc] initWithFrame: [testPanView frame]; [imageview setImage: image]; [testPanView addSubview: imageview]; using * panRecognizer = [[using alloc] initWithTarget: self action: @ selector (handlePan :)]; [panRecognizer identifier: 1]; [panRecognizer identifier: 1]; [panRecognizer setDelegate: self]; [testPanView addGestureRecognizer: panRecognizer]; [self. view addSubview: testPanView]; testPanTableView = [[UITableView alloc] initWithFrame: CGRectMake (118,121,100,100) style: Custom]; optional * panRecognizer2 = [[Alibaba alloc] initWithTarget: self action: @ selector (handlePan2 :)]; [panRecognizer2 keys: 1]; [panRecognizer2 keys: 1]; [panRecognizer2 setDelegate: self]; [testPanTableView keys: panRecognizer2]; [self. view addSubview: testPanTableView];}-(void) handlePan :( UIPanGestureRecognizer *) recognizer {CGPoint translatedPoint = [recognizer translationInView: self. view]; NSLog (@ "gesture translatedPoint is % @", NSStringFromCGPoint (translatedPoint); CGFloat x = recognizer. view. center. x + translatedPoint. x; CGFloat y = recognizer. view. center. y + translatedPoint. y; recognizer. view. center = CGPointMake (x, y); NSLog (@ "pan gesture testPanView moving is % @, % @", NSStringFromCGPoint (recognizer. view. center), NSStringFromCGRect (recognizer. view. frame); [recognizer setTranslation: CGPointMake (0, 0) inView: self. view];}-(void) handlePan2 :( alert *) recognizer {// NSLog (@ "gesture translatedPoint xxoo"); CGPoint translatedPoint = [recognizer translationInView: self. view]; if ([(UIPanGestureRecognizer *) recognizer state] = UIGestureRecognizerStateBegan) {firstX = recognizer. view. center. x; firstY = recognizer. view. center. y; NSLog (@ "self. view bounds is % @ ", NSStringFromCGRect (self. view. bounds); NSLog (@ "pan gesture testPanView begin is % @, % @", NSStringFromCGPoint ([recognizer view]. center), NSStringFromCGRect ([recognizer view]. frame);} if ([(UIPanGestureRecognizer *) recognizer state] = UIGestureRecognizerStateChanged) {CGFloat x = firstX + translatedPoint. x; CGFloat y = firstX + translatedPoint. y; if (x <recognizer. view. width/2.0) {x = recognizer. view. width/2.0;} else if (x + recognizer. view. width/2.0> self. view. width) {x = self. view. width-recognizer. view. width/2.0;} if (y <recognizer. view. size/2.0) {y = recognizer. view. height/2.0;} else if (y + recognizer. view. height/2.0> self. view. height) {y = self. view. height-recognizer. view. height/2.0;} NSLog (@ "gesture translatedPoint moving is % @", NSStringFromCGPoint (translatedPoint); recognizer. view. center = CGPointMake (x, y);} if ([(UIPanGestureRecognizer *) recognizer state] = UIGestureRecognizerStateEnded) | ([(UIPanGestureRecognizer *) recognizer state] = UIGestureRecognizerStateCancelled) {CGFloat x = recognizer. view. center. x; CGFloat y = recognizer. view. center. y; if (x> firstX) {x = self. view. width-recognizer. view. width/2.0;} else {x = recognizer. view. width/2.0;} if (y> firstY) {y = self. view. height-recognizer. view. height/2.0;} else {y = recognizer. view. height/2.0;} CGFloat velocityX = (0.2 * [recognizer velocityInView: self. view]. x); [UIView beginAnimations: nil context: NULL]; [UIView setAnimationDuration: ABS (velocityX * 0.00002 + 0.2)]; [UIView setAnimationCurve: UIViewAnimationCurveEaseOut]; recognizer. view. center = CGPointMake (x, y); [UIView commitAnimations]; NSLog (@ "gesture translatedPoint end is % @", NSStringFromCGPoint (translatedPoint )); NSLog (@ "pan gesture testPanView end is % @, % @", NSStringFromCGPoint ([recognizer view]. center), NSStringFromCGRect ([recognizer view]. frame ));}}

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: info-contact@alibabacloud.com 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.