我想制作一个滑动按钮,因此当用户从右向左滑动按钮时,按钮也会从右向左绘制,最后它会推动另一个视图,任何人都可以为其提供示例代码。
答案 0 :(得分:1)
// make button
UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 50, 50)];
[btn addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
[self.view addSubview:btn];
// here swipe buttons
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
// get the touch
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
CGPoint moveBtnCenter = CGPointMake(button.center.x + delta_x,
button.center.y + delta_y);
button.center = moveBtnCenter;
NSArray* allSubViews = [self.view subviews];
for (UIView*subView in allSubViews)
{
if ([subView isKindOfClass:[UIButton class]])
{
CGPoint subViewCenter = subView.center;
if (CGPointEqualToPoint(moveBtnCenter, subViewCenter))
{
subView.center = previousLocation;
}
}
}
}