我想使用 touchesbegan、touchesmoved 和 touchesended 方法从我的视图控制器中移动一个对象。它按我想要的方式工作(如果它移动的距离不够大,它将被放置到它的初始位置)但是拖动的动画并不流畅。任何人都可以帮助我,使其成为流畅的拖动动作吗?
这是我的代码:
#pragma mark -
#pragma mark Touch Gestures
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
if (touch.view == _topCard) {
CGPoint touchPoint = [touch locationInView:_topCard];
touchCenterOffset = CGPointMake([touch view].center.x - touchPoint.x,
[touch view].center.y - touchPoint.y);
[self animateFirstTouchAtPoint:touchPoint];
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
if (touch.view == _topCard) {
CGPoint location = [touch locationInView:_topCard];
CGPoint pointOnTable = [_topCard convertPoint:[touch locationInView:_topCard] toView:_topCard.superview];
CGPoint newCenter = CGPointMake(location.x + touchCenterOffset.x,
location.y + touchCenterOffset.y);
[touch view].center = newCenter;
if (pointOnTable.x > 180) {
NSLog(@"Aktionismus");
} else {
NSLog(@"KEIN Aktionismus");
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
if (touch.view == _topCard) {
CGPoint pointOnTable = [_topCard convertPoint:[touch locationInView:_topCard] toView:_topCard.superview];
if (pointOnTable.x > 180) {
NSLog(@"Aktionismus");
} else {
[self animatePutBackTopCard:FLIP_DURATION];
}
}
}
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch* touch = [touches anyObject];
if (touch.view == _topCard) {
[self animatePutBackTopCard:FLIP_DURATION];
}
}
#pragma mark -
#pragma mark Animations
- (void)animateFirstTouchAtPoint:(CGPoint)touchPoint {
/*[UIView animateWithDuration:GROW_ANIMATION_DURATION_SECONDS animations:^{
_topCard.transform = CGAffineTransformScale(_topCard.transform, 1.2, 1.2);
}];*/
}
- (void)animatePutBackTopCard:(NSTimeInterval)duration {
[UIView animateWithDuration:duration animations:^{
CGPoint originalCenter = CGPointMake(ORIGIN_CENTER_X,ORIGIN_CENTER_Y);
_topCard.center = originalCenter;
}];
}
非常感谢!