Reputation: 6164
In my iPhone App I want to play with touch events. Here my requirement is as shown in figure:
I am creating Button 2
on the touch drag Exit
event of Button 1
. Here is the code
-(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];
}
Here I am able to create button 2 successfully but the problem is I am not able to drag and move the button2 in single touch events means while dragging outside on button 1 it is creating button 2 but to move button 2 i have to take my finger off the screen and on next touch on button 2 i could move the button 2.
My question is, how to create button2 and move it across screen on single touch cycle?
Upvotes: 0
Views: 1048
Reputation: 7461
Set button UserInteraction Disable ands use following code..
[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
Hope, this will help you
Upvotes: 1