0

在我的 iPhone 应用程序中,我想玩触摸事件。这里我的要求如图:

在此处输入图像描述

Button 2正在touch drag Exit创建Button 1. 这是代码

 -(IBAction)addView:(id)sender{

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=count;
count++;
    [button addTarget:self action:@selector(imageTouch:withEvent:) forControlEvents:UIControlEventTouchDown];
    [button addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
    [button addTarget:self action:@selector(imageTouchCancel:withEvent:) forControlEvents:UIControlEventTouchUpInside];

button.frame=CGRectMake(shape1.frame.origin.x-30,shape1.frame.origin.y-40, 100, 100);

   [button setImage:[UIImage imageNamed:imgname] forState:UIControlStateNormal];
   [button addSubview:close];
  [baseview addSubview:button];

 }

在这里,我能够成功创建按钮 2,但问题是我无法在单点触摸事件中拖动和移动按钮 2,这意味着在按钮 1 上向外拖动时,它正在创建按钮 2,但要移动按钮 2,我必须用手指关闭屏幕并在下一次触摸按钮 2 时,我可以移动按钮 2。

我的问题是,如何创建 button2 并在单次触摸循环中将其移动到屏幕上?

4

1 回答 1

1

设置按钮用户交互禁用并使用以下代码..

[button2 setUserInteractionEnabled:NO];

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
button2.center = location;
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
 }

在视图上拖动图像的示例代码

希望对你有帮助

于 2012-04-30T12:45:25.600 回答