可拖动按钮

    BOOL _isMove;


    _isMove=0;





UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

btn.frame =CGRectMake(10,10, 50, 50); 

btn setTitle:@"触摸"forState:UIControlStateNormal];

btn setTitle:@"移动"forState:UIControlEventTouchDown];

btn addTarget:selfaction:@selector(dragMoving:withEvent: )forControlEvents:UIControlEventTouchDragInside];

[btnaddTarget:selfaction:@selector(dragEnded:withEvent: )forControlEvents:UIControlEventTouchUpInside |

     UIControlEventTouchUpOutside];

[self.viewaddSubview:btn];


- (void) dragMoving: (UIControl *) c withEvent:ev//移动

{

    _isMove=1;


    c.center = [[[evallTouches] anyObject]locationInView:self.view];

}


- (void) dragEnded: (UIControl *) c withEvent:ev//开始和停止

{

    if (!_isMove) {

        NSLog(@"doWhat");


    }

    _isMove=0;


}



猜你喜欢

转载自blog.csdn.net/lichuanliangios/article/details/48161565