2012-04-30 27 views
0

在我的iPhone應用程序中,我想玩觸摸事件。這裏我的要求是:如圖所示。 enter image description hereiPhone App:觸摸事件並將控制權轉移給其他對象

我正在創建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成功的,但問題是我不能夠在單點觸摸事件拖動和移動BUTTON2的同時拖動外面按鈕1它正在創建按鈕2手段但要移動按鈕2,我必須將手指從屏幕上移開,並在下一次觸摸按鈕2時,我可以移動按鈕2.

我的問題是如何在單觸摸週期中創建button2並在屏幕上移動它?

請幫助和建議。

感謝

+0

我認爲你需要使用'Touch'方法並檢測您正在觸摸哪個對象。如果它是button2,那麼你必須開始移動它touchMoved被調用。讓我們玩!讓我知道你是否需要更多的幫助。 –

回答

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

希望,這將幫助你