iOSドラッグ可能buttonについて

1338 ワード

今日はまた暇で卵が痛くて、buttonのドラッグ事件について研究して、実はとても简単で、いくつかの言叶が必要で、くだらないことは多く言わないで、コードをつけます
viewcontrollerについてhの中身
@property(nonatomic,strong)UIButton *button;

viewcontrollerについてmの中のものはまずviewDidLoadの中の
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button.frame = CGRectMake(100, 100, 50, 50);
    self.button.backgroundColor = [UIColor redColor];
    [self.button setTitle:@" " forState:UIControlStateNormal];
    [self.button setTitle:@" " forState:UIControlEventTouchDown];
    [self.button addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];
    [self.button addTarget:self action:@selector(dragEnded:withEvent: )forControlEvents:UIControlEventTouchUpOutside];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.button];
    }


そして方法
- (void) dragMoving: (UIControl *) c withEvent:ev
{
    c.center = [[[ev allTouches] anyObject] locationInView:self.view];
}

- (void) dragEnded: (UIControl *) c withEvent:ev
{
    c.center = [[[ev allTouches] anyObject] locationInView:self.view];
}

-(void)buttonAction:(UIButton *)sender
{
    NSLog(@" ");
}


はい、簡単です.