メソッド touchesbegan、touchesmoved、および touchesended を使用して、ViewController からオブジェクトを移動したいと考えています。それは私が望むように機能します(移動距離が十分に大きくない場合、最初の位置に配置されます)が、ドラッグのアニメーションはスムーズではありません。誰かが私を助けて、流暢なドラッグの動きにすることができますか?
これは私のコードです:
#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;
}];
}
どうもありがとう!