Ios 触摸后UI元素出现问题
在iOS应用程序中,我有两个UI元素跟随用户的触摸。问题是,如果触摸速度加快,UI元素就会停止跟随。 它还确保UI元素不能移动到特定边界之外 下面是我用来移动UI元素的代码,运行在移动的触摸屏上:Ios 触摸后UI元素出现问题,ios,objective-c,xcode,Ios,Objective C,Xcode,在iOS应用程序中,我有两个UI元素跟随用户的触摸。问题是,如果触摸速度加快,UI元素就会停止跟随。 它还确保UI元素不能移动到特定边界之外 下面是我用来移动UI元素的代码,运行在移动的触摸屏上: // Move the pinch UI around - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject];
// Move the pinch UI around
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLoc = [touch locationInView:self.view];
CGPoint imageHeight = CGPointMake((_pinchUI_top.frame.origin.x)+(_pinchUI_top.frame.size.width/2), size_OriginalImage.height);
// Top line
if (CGRectContainsPoint([_pinchUI_top frame], [touch locationInView:self.view])) {
CGPoint touchLocY = CGPointMake((_pinchUI_top.frame.origin.x)+(_pinchUI_top.frame.size.width/2), touchLoc.y);
_pinchUI_top.center = touchLocY;
// If outside of image, stop
if (touchLocY.y > imageHeight.y) {
_pinchUI_top.center = imageHeight;
}
else {
pixelsFromBottom = touchLoc.y;
}
// Bottom line
} else if (CGRectContainsPoint([_pinchUI_bottom frame], [touch locationInView:self.view])) {
CGPoint touchLocY = CGPointMake((_pinchUI_bottom.frame.origin.x)+(_pinchUI_bottom.frame.size.width/2), touchLoc.y);
_pinchUI_bottom.center = touchLocY;
// If outside of image, stop
if (touchLocY.y > imageHeight.y) {
_pinchUI_bottom.center = imageHeight;
}
else {
pixelsFromTop = touchLoc.y;
}
}
}
_pinchUI_top和pinchUI_bottom是两个UI元素。使用TouchesBegind事件获取原始位置并处理top或bottom,因此不要在touchesMoved中检查此项,而是在touch事件启动时执行此项操作。然后在TouchsMoved事件中,您可以移动相应的图像
float topY;
float bottomY;
bool touchValid;
bool touchTop;
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLoc = [touch locationInView:self.view];
float touchY = touchLoc.y;
touchValid = NO;
if (CGRectContainsPoint([_pinchUI_top frame], [touch locationInView:self.view])) {
topY = touchY;
touchTop = YES;
touchValid = YES;
} else if (CGRectContainsPoint([_pinchUI_bottom frame], [touch locationInView:self.view])){
bottomY = touchY;
touchTop = NO;
touchValid = YES;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLoc = [touch locationInView:self.view];
CGPoint imageHeight = CGPointMake((_pinchUI_top.frame.origin.x)+(_pinchUI_top.frame.size.width/2), size_OriginalImage.height);
if (touchValid) {
// Top line
if (touchTop) {
CGPoint touchLocY = CGPointMake((_pinchUI_top.frame.origin.x)+(_pinchUI_top.frame.size.width/2), touchLoc.y);
_pinchUI_top.center = touchLocY;
// If outside of image, stop
if (touchLocY.y > imageHeight.y) {
_pinchUI_top.center = imageHeight;
}
else {
pixelsFromBottom = touchLoc.y;
}
// Bottom line
} else if (!touchTop) {
CGPoint touchLocY = CGPointMake((_pinchUI_bottom.frame.origin.x)+(_pinchUI_bottom.frame.size.width/2), touchLoc.y);
_pinchUI_bottom.center = touchLocY;
// If outside of image, stop
if (touchLocY.y > imageHeight.y) {
_pinchUI_bottom.center = imageHeight;
}
else {
pixelsFromTop = touchLoc.y;
}
}
}
}
这应该可以解决它