Drag specific object
I have a problem with dragging object in iPhone sdk , my UIImageView outlet is red and I don't know why the dragging doesn't work ! but when remove the if line my code works fine , here is the code :
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event al开发者_JS百科lTouches]anyObject];
if ([touch view] == red) {
CGPoint location = [touch locationInView:self.view];
red.center = location;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
When you override touchesBegan of a UIView sub class, only the touches delivered to that UIView are handled there.
For its subviews you need to set UserInteractionEnabled to TRUE. And then make a subclass for your subviews through which you can delegate the touch handling back to your SuperView.
If that sounds complex, just use - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Hi I did it with below code. I have to drag imageEnd in specific horizontal and vertical way so I bound them as below.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: touch.view];
location.x=510.0;
[imgEndPoint setUserInteractionEnabled:TRUE];
if ([touch view] == imgEndPoint) {
if (location.y < 710.0 && location.y > 75.0) {//image move between this coordinate
imgEndPoint.center = location;
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == imgEndPoint) {
CGPoint location = [touch locationInView: self.view];
location.x=510.0;
if (location.y < 710.0 && location.y > 75.0) {
imgEndPoint.center = location;
[self getPixcel];
}
[imgEndPoint setUserInteractionEnabled:TRUE];
}
}
精彩评论