Illustrate the implementation of drag view in IOS development _ios

Source: Internet
Author: User
Tags touch uikit

Preliminary knowledge
iOS handles touch actions on the screen, mainly involving the following methods:

Copy Code code as follows:

Touchesbegan:withevent://Touch screen at the beginning of the call
Touchesmoved:withevent://During move is called
Touchesended:withevent://The action is called at the end
Touchescancelled:withevent:


From the name of the method you can clearly see when the method is called, the last one is special. Touchescancelled:withevent: Called when the cocoa touch must be interrupted by a system that responds to persistent touches.
We just have to rewrite these methods to do what we want to do.

How do I implement a drag view?
1. Set the Userinteractionenabled property to Yes to allow user interaction.
2. Record the starting point at the beginning of the touch action.
3. During the move, calculate the difference between the current position coordinate and the starting point, that is, the offset, and move the view center point to the offset size.
4. Limit the x coordinates, and y coordinates respectively, to ensure that the user can not be the view of the screen

Note: The reason for limiting the x coordinates to the y coordinates is that you can drag down even if you can't drag to the right.

In fact, the function is relatively simple, is the iOS gesture animation drag. Take a look at the basic wording:

1. Register Drag Animation

Copy Code code as follows:

Uipangesturerecognizer * Pangesturerecognizer = [[Uipangesturerecognizer alloc] Initwithtarget:self
Action: @selector (dohandlepanaction:)];
[Self.vlight Addgesturerecognizer:pangesturerecognizer];


Note: Vlight is the view subclass to which you want to add a drag.

2. Drag handler function

Copy Code code as follows:

-(void) Dohandlepanaction: (Uipangesturerecognizer *) paramsender{

Cgpoint point = [Paramsender TranslationInView:self.view];
NSLog (@ "X:%F; Y:%f ", POINT.X,POINT.Y);

ParamSender.view.center = Cgpointmake (paramsender.view.center.x + point.x, paramsender.view.center.y + point.y);
[Paramsender settranslation:cgpointmake (0, 0) InView:self.view];

}


Implementation code
A case study of Uiimageview of sub-class

Copy Code code as follows:

#import <UIKit/UIKit.h>

@interface Gragview:uiimageview
{
Cgpoint StartPoint;
}
@end

#import "GragView.h"

@implementation Gragview

-(ID) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
Initialization code
Allow user interaction
self.userinteractionenabled = YES;
}
return self;
}

-(ID) initwithimage: (UIImage *) image
{
self = [super Initwithimage:image];
if (self) {
Allow user interaction
self.userinteractionenabled = YES;
}
return self;
}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Save Touch Start point location
Cgpoint point = [[touches anyobject] locationinview:self];
StartPoint = point;

The view is placed in the front
[[Self Superview] bringsubviewtofront:self];
}

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
calculated displacement = Current position-start position
Cgpoint point = [[touches anyobject] locationinview:self];
float dx = point.x-startpoint.x;
float dy = point.y-startpoint.y;

Calculate the moved View Center point
Cgpoint newcenter = cgpointmake (self.center.x + dx, self.center.y + dy);


/* Restrict the user from having the view out of the screen *
float Halfx = Cgrectgetmidx (self.bounds);
X-Coordinate left boundary
newcenter.x = MAX (Halfx, newcenter.x);
X-Coordinate right boundary
newcenter.x = MIN (Self.superview.bounds.size.width-halfx, newcenter.x);

Y coordinate the same
float Halfy = Cgrectgetmidy (self.bounds);
Newcenter.y = MAX (Halfy, NEWCENTER.Y);
Newcenter.y = MIN (Self.superview.bounds.size.height-halfy, NEWCENTER.Y);

Move View
Self.center = Newcenter;
}

/*
Only override drawrect:if you perform custom drawing.
An empty implementation adversely affects performance during.
-(void) DrawRect: (cgrect) rect
{
Drawing Code
}
*/

@end


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.