在我的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并在单个触摸循环中将其移动到屏幕上?
请帮助和建议。
由于
答案 0 :(得分:1)
设置按钮UserInteraction禁用并使用以下代码..
[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
希望,这会对你有所帮助