我想使用方法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;
}];
}
非常感谢!
答案 0 :(得分:4)
最后我想出了我的问题所在:
我没有跟踪超视图中对象的移动,而是跟踪对象上的接触点。
这是经过调整的代码,可以顺利拖动工作:
- (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;
}
}
答案 1 :(得分:1)
看看我是如何移动一个物体并且它正在顺利运行:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.superview];
float difx = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;
float dify = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify);
self.transform = newTransform1;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.superview];
float difx = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;
float dify = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify);
self.transform = newTransform1;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
我认为你可以适应你的需求。
答案 2 :(得分:1)
我使用以下代码拖动a custom UIView:
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self];
CGPoint previous = [touch previousLocationInView:self];
if (!CGAffineTransformIsIdentity(self.transform)) {
location = CGPointApplyAffineTransform(location, self.transform);
previous = CGPointApplyAffineTransform(previous, self.transform);
}
self.frame = CGRectOffset(self.frame,
(location.x - previous.x),
(location.y - previous.y));
}
答案 3 :(得分:1)
如其他答案所述,直接操纵视图框架是不好的做法。例如,使用UIDynamics时会导致非常奇怪的行为。 你应该设置视图的中心。
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch {
let p1 = touch.locationInView(self.superview)
let p0 = touch.previousLocationInView(self.superview)
let translation = CGPointMake(p1.x - p0.x, p1.y - p0.y);
center = CGPointMake(center.x + translation.x, center.y + translation.y)//set views center
}
}
答案 4 :(得分:0)
对于拖动,我建议改为使用panGestureRecognizer。
可以找到信息here