IOS development (70) drag gesture

Source: Internet
Author: User

1 Preface
Use UIPanGestureRecognizer to drag and drop layers.


2. code example
ZYViewController. m

 

[Plain]
@ Synthesize helloWorldLabel;
@ Synthesize panGestureRecognizer;
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
/* Let's first create a label */
CGRect labelFrame = CGRectMake (0.0f,/* X */
0.0f,/* Y */
150366f,/* width */
100366f);/* high */
Self. helloWorldLabel = [[UILabel alloc] initWithFrame: labelFrame];
Self. helloWorldLabel. text = @ "Hello World ";
Self. helloWorldLabel. backgroundColor = [UIColor blackColor];
Self. helloWorldLabel. textColor = [UIColor whiteColor];
Self. helloWorldLabel. textAlignment = NSTextAlignmentCenter;
// Make sure that the label can interact with attributes so that the method can be activated.
Self. helloWorldLabel. userInteractionEnabled = YES;
[Self. view addSubview: self. helloWorldLabel];
// Create a drag gesture
Self. panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self
Action: @ selector (handpolicangestures :)];
// Only one finger is allowed, regardless of the maximum or minimum.
Self. panGestureRecognizer. minimumNumberOfTouches = 1;
Self. panGestureRecognizer. maximumNumberOfTouches = 1;
[Self. helloWorldLabel addGestureRecognizer: self. panGestureRecognizer];
}
 
-(Void) handpolicangestures :( UIPanGestureRecognizer *) paramSender {
If (paramSender. state! = UIGestureRecognizerStateEnded & paramSender. state! = UIGestureRecognizerStateFailed ){
// Use the locationInView method to obtain the coordinates of the gesture.
CGPoint location = [paramSender locationInView: paramSender. view. superview];
ParamSender. view. center = location;
}
}

@ Synthesize helloWorldLabel;
@ Synthesize panGestureRecognizer;

-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
/* Let's first create a label */
CGRect labelFrame = CGRectMake (0.0f,/* X */
0.0f,/* Y */
150366f,/* width */
100366f);/* high */
Self. helloWorldLabel = [[UILabel alloc] initWithFrame: labelFrame];
Self. helloWorldLabel. text = @ "Hello World ";
Self. helloWorldLabel. backgroundColor = [UIColor blackColor];
Self. helloWorldLabel. textColor = [UIColor whiteColor];
Self. helloWorldLabel. textAlignment = NSTextAlignmentCenter;
// Make sure that the label can interact with attributes so that the method can be activated.
Self. helloWorldLabel. userInteractionEnabled = YES;
[Self. view addSubview: self. helloWorldLabel];
// Create a drag gesture
Self. panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget: self
Action: @ selector (handpolicangestures :)];
// Only one finger is allowed, regardless of the maximum or minimum.
Self. panGestureRecognizer. minimumNumberOfTouches = 1;
Self. panGestureRecognizer. maximumNumberOfTouches = 1;
[Self. helloWorldLabel addGestureRecognizer: self. panGestureRecognizer];
}

-(Void) handpolicangestures :( UIPanGestureRecognizer *) paramSender {
If (paramSender. state! = UIGestureRecognizerStateEnded & paramSender. state! = UIGestureRecognizerStateFailed ){
// Use the locationInView method to obtain the coordinates of the gesture.
CGPoint location = [paramSender locationInView: paramSender. view. superview];
ParamSender. view. center = location;
}
}
Running result

 
 


Result After dragging

 
 


 

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.