当前位置:编程学习 > wap >>

IOS开发(70)之拖拽手势

1 前言
利用 UIPanGestureRecognizer 这个手势识别器, 来实现图层的拖拽。


2 代码实例
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 */ 
                                   150.0f, /* 宽 */ 
                                   100.0f); /* 高 */ 
    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; 
    //确保label可以互动的属性,以便可以激活其方法 
    self.helloWorldLabel.userInteractionEnabled = YES; 
    [self.view addSubview:self.helloWorldLabel]; 
    //创建拖拽手势 
    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self 
                                                                action:@selector(handlePanGestures:)]; 
    //无论最大还是最小都只允许一个手指 
    self.panGestureRecognizer.minimumNumberOfTouches = 1; 
    self.panGestureRecognizer.maximumNumberOfTouches = 1; 
    [self.helloWorldLabel addGestureRecognizer:self.panGestureRecognizer]; 

 
- (void) handlePanGestures:(UIPanGestureRecognizer*)paramSender{ 
    if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){ 
        //通过使用 locationInView 这个方法,来获取到手势的坐标 
        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 */
                                   150.0f, /* 宽 */
                                   100.0f); /* 高 */
    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;
    //确保label可以互动的属性,以便可以激活其方法
    self.helloWorldLabel.userInteractionEnabled = YES;
    [self.view addSubview:self.helloWorldLabel];
    //创建拖拽手势
    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                action:@selector(handlePanGestures:)];
    //无论最大还是最小都只允许一个手指
    self.panGestureRecognizer.minimumNumberOfTouches = 1;
    self.panGestureRecognizer.maximumNumberOfTouches = 1;
    [self.helloWorldLabel addGestureRecognizer:self.panGestureRecognizer];
}

- (void) handlePanGestures:(UIPanGestureRecognizer*)paramSender{
    if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){
        //通过使用 locationInView 这个方法,来获取到手势的坐标
        CGPoint location = [paramSender locationInView:paramSender.view.superview];
        paramSender.view.center = location;
    }
}
运行结果

 \
 


拖拽后结果

 \
 


 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,