我正在尝试创建一个控件,用户可以在其中触摸和移动框架内的按钮。这是我的代码。
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:button] anyObject];
// get delta
CGPoint previousLocation = [touch previousLocationInView:button];
CGPoint location = [touch locationInView:button];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
// move button
button.center = CGPointMake(button.center.x + delta_x,
button.center.y + delta_y);
}
我可以移动按钮(通过触摸和拖动),但是如何限制按钮,使其只能在矩形框架内向左/向右移动。