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