iPhone应用程序:触摸事件并将控制转移到其他对象
在我的iPhone应用程序中,我想玩触摸事件。这里我的需求如图所示: 我正在iPhone应用程序:触摸事件并将控制转移到其他对象,iphone,objective-c,uibutton,uitouch,Iphone,Objective C,Uibutton,Uitouch,在我的iPhone应用程序中,我想玩触摸事件。这里我的需求如图所示: 我正在按钮1的触摸拖动退出事件上创建按钮2。这是密码 -(IBAction)addView:(id)sender{ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.tag=count; count++; [button addTarget:self action:@selector(imageTouch:withEve
按钮1
的触摸拖动退出事件上创建按钮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
我的问题是,如何创建按钮2并在单次触摸循环中在屏幕上移动它?设置按钮用户交互禁用并使用以下代码
[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];
}
希望,这将帮助您我认为您需要使用Touch
方法来检测您正在触摸的对象。如果它是按钮2,那么您必须开始移动它,称之为touchesMoved。让我们玩吧!如果你需要更多的帮助,请告诉我。