Hi there,
I have a UIScrollView which contains some small UIView subclass. UIScrollView is scroll enabled, and I want each UIView can be dragged inside UIScrollView freely.
My UIView subclass has this method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] != self) {
return;
}
CGPoint touchPoint = [touch locationInView:self.superview];
originalX = self.center.x;
originalY = self.center.y;
offsetX = originalX - touchPoint.x;
offsetY = originalY - touchPoint.y;
[self.superview bringSubviewToFront:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
CGPoint location = [touch locationInView:self.superview];
CGFloat x = location.x + offsetX;
CGFloat y = location.y + offsetY;
self.center = CGPointMake(x, y);
return;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == self) {
self.center = CGPointMake(originalX, originalY);
}
}
I found touchesCancelled:withEvent will be called each time I just drag UIView several pixels. But these codes will work correctly if it is subclass of UIControl. Why?
Thanks in advance!