gpt4 book ai didi

ios - 使用 touchesbegan、touchesmoved 和 touchesended 不能使拖动流畅地进行动画处理

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:08:13 24 4
gpt4 key购买 nike

我想使用方法 touchesbegan、touchesmoved 和 touchesended 从我的 View Controller 中移动一个对象。它按照我想要的方式工作(如果它移动的距离不够大,它将被放置到它的初始位置)但是拖动的动画不流畅。任何人都可以帮助我,让它成为一个流畅的拖动 Action 吗?

这是我的代码:

#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;
}];
}

非常感谢!

最佳答案

终于找到问题所在了:

我没有跟踪父 View 中对象的移动,而是跟踪对象上的接触点。

这是经过调整的代码,可以很好地平滑拖动:

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
if (touch.view == _topCard) {
translation = [touch locationInView:_topCard.superview];
}

}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];

if (touch.view == _topCard) {
CGPoint newTranslation = [touch locationInView:_topCard.superview];
_topCard.frame = CGRectOffset(_topCard.frame, newTranslation.x - translation.x, newTranslation.y - translation.y);
translation = newTranslation;

}
}

关于ios - 使用 touchesbegan、touchesmoved 和 touchesended 不能使拖动流畅地进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com