【问题标题】:iPhone App: touch event and transfer control to other objectiPhone App:触摸事件并将控制权转移到其他对象
【发布时间】:2012-04-30 12:31:35
【问题描述】:

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

我在Button 1touch drag Exit 事件上创建Button 2。这是代码

 -(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 并在单次触摸循环中将其在屏幕上移动?

【问题讨论】:

  • 我认为您需要使用Touch 方法并检测您正在触摸的对象。如果是 button2,那么您必须开始移动它,这会调用 touchesMoved。让我们玩!如果您需要更多帮助,请告诉我。

标签: iphone objective-c uibutton uitouch


【解决方案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];
 }

Sample code for dragging image over the view

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多