`
iaiai
  • 浏览: 2146628 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

按钮拖动

 
阅读更多
@implementation ViewController{
    UIButton* btn;
    CGPoint lastPoint;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [super viewDidLoad];
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setBackgroundColor:[UIColor blackColor]];
    btn.frame = CGRectMake(10, 10, 150, 50);
    
    [btn setTitle:@"拖动" forState:UIControlStateNormal];
    
    UIPanGestureRecognizer* movePress = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragButton:)];
    [btn addGestureRecognizer:movePress];
    
    [self.view addSubview:btn];
}

- (void)dragButton:(UILongPressGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.view];
    
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            lastPoint = point;
            break;
        case UIGestureRecognizerStateChanged:{
            CGFloat offX = point.x - lastPoint.x;
            CGFloat offY = point.y - lastPoint.y;
            [btn setCenter:CGPointMake(btn.center.x + offX, btn.center.y + offY)];
            lastPoint = point;
        }
            break;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics