Create a view principle and implementation in iOS that can be dragged (with code)

Source: Internet
Author: User

Sometimes we will need to drag view;uiview on the interface that is inherited from Uiresponder, so you can respond to touch-related events.

The focus is on the following set of methods:

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event


-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event


-(void) touchesended: (Nsset *) touches withevent: (Uievent *) Event//Touch events end, if you need to automatically dock view to a location, implement this method


-(void) touchescancelled: (Nsset *) touches withevent: (Uievent *) Event//external factor cancellation touch event, such as access to the phone, for special processing


For the top two methods must be implemented, the following two methods are used to do some additional requirements or processing use, if only to implement the drag view can not be implemented.


Idea 1: Create a subclass of UIView (or the control you need) to implement the above method in the class.

Idea 2: Implement the above method in your corresponding Viewcontroller (hold the view you want to drag in Viewcontroller to control it), and achieve a similar purpose, but the scope of the touch will be the entire Viewcontroller view , you need to make a corresponding judgment in Touchesbegan (you can get the information about the view from the Uitouch) in order to achieve a touch that is fixed within a small window.

Both of the ideas are feasible, according to your actual situation to make choices, there is no problem.

Here is the code (a simple implementation of the subclass, you can also make the corresponding changes in the Viewcontroller):

@interface Toucheaglview ()
@property (Assign, nonatomic) Cgpoint beginpoint;
@end
@implementation Toucheaglview

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch *touch = [touches anyobject];
Self.beginpoint = [Touch locationinview:self];
[Super Touchesbegan:touches Withevent:event];
}

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch *touch = [touches anyobject];
Cgpoint currentlocation = [Touch locationinview:self];
CGRect frame = self.frame;
Frame.origin.x + = Currentlocation.x-self.beginpoint.x;
FRAME.ORIGIN.Y + = Currentlocation.y-self.beginpoint.y;
Self.frame = frame;
}


The problem with the above code is that his touch moves beyond the screen, and you'll find that you can drag the view section to the outside of the screen. Then we need an advanced implementation:

Note: This version is based on Viewcontroller implementation, does not subclass View;self.localview is the small window you hold, Beginpoint need you to define in Viewcontroller

-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
if (!self.isinview)//Only when the touch view is a small window, we respond to touch, or return directly
{
Return
}

Uitouch *touch = [touches anyobject];
Cgpoint currentposition = [Touch LocationInView:self.localView];
Offset amount
float OffsetX = currentposition.x-beginpoint.x;
float OffsetY = Currentposition.y-beginpoint.y;
The center coordinates after the move
Self.localView.center = Cgpointmake (self.localview.center.x + offsetX, self.localview.center.y + OffsetY);

X-axis, left and right limit coordinates
if (Self.localview.center.x > (SELF.LOCALVIEW.SUPERVIEW.FRAME.SIZE.WIDTH-SELF.LOCALVIEW.FRAME.SIZE.WIDTH/2))
{
CGFloat x = SELF.LOCALVIEW.SUPERVIEW.FRAME.SIZE.WIDTH-SELF.LOCALVIEW.FRAME.SIZE.WIDTH/2;
Self.localView.center = Cgpointmake (x, Self.localview.center.y + OffsetY);
}
else if (Self.localview.center.x < SELF.LOCALVIEW.FRAME.SIZE.WIDTH/2)
{
CGFloat x = SELF.LOCALVIEW.FRAME.SIZE.WIDTH/2;
Self.localView.center = Cgpointmake (x, Self.localview.center.y + OffsetY);
}

Y-axis Upper and lower limit coordinates
if (Self.localview.center.y > (SELF.LOCALVIEW.SUPERVIEW.FRAME.SIZE.HEIGHT-SELF.LOCALVIEW.FRAME.SIZE.HEIGHT/2))
{
CGFloat x = self.localview.center.x;
CGFloat y = SELF.LOCALVIEW.SUPERVIEW.FRAME.SIZE.HEIGHT-SELF.LOCALVIEW.FRAME.SIZE.HEIGHT/2;
Self.localView.center = Cgpointmake (x, y);
}
else if (self.localview.center.y <= SELF.LOCALVIEW.FRAME.SIZE.HEIGHT/2)
{
CGFloat x = self.localview.center.x;
CGFloat y = SELF.LOCALVIEW.FRAME.SIZE.HEIGHT/2;
Self.localView.center = Cgpointmake (x, y);
}
}


-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch *touch = [touches anyobject];
if (touch.view.frame.size.width = = 120)//120 is the width of the small window (for simplicity, a hard-coded example is used here) to determine the touch range; we only respond to touch when the view is a small window
{
Self.isinview = YES;
}
Else
{
Self.isinview = NO;
}
Beginpoint = [Touch LocationInView:self.localView];

[Super Touchesbegan:touches Withevent:event];
}







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.