【问题标题】:iPhone - Move object along touch and drag pathiPhone - 沿触摸和拖动路径移动对象
【发布时间】:2012-03-29 08:10:57
【问题描述】:

我正在开发一个简单的动画,其中UIImageView 沿着UIBezierPath 移动,现在我想为移动的UIImageView 提供用户交互,以便用户可以通过触摸UIImageView 来引导UIImageView 和在屏幕上拖动UIImageview

【问题讨论】:

  • 先生,我和你有同样的问题,如果你这样做,请帮助我。

标签: ios objective-c cocoa-touch core-animation iphone-sdk-3.0


【解决方案1】:

改变touchesMoved:withEvent:中image view位置的frame。

编辑:一些代码

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch.view isEqual: self.view] || touch.view == nil) {
        return;
    }

    lastLocation = [touch locationInView: self.view];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [[event allTouches] anyObject];
    if ([touch.view isEqual: self.view]) {
        return;
    }

    CGPoint location = [touch locationInView: self.view];

    CGFloat xDisplacement = location.x - lastLocation.x;
    CGFloat yDisplacement = location.y - lastLocation.y;

    CGRect frame = touch.view.frame;
    frame.origin.x += xDisplacement;
    frame.origin.y += yDisplacement;
    touch.view.frame = frame;
    lastLocation=location;
}

您还应该实现touchesEnded:withEvent:touchesCanceled:withEvent:

【讨论】:

  • 好的,但是如何识别触摸是否在特定对象上?
  • 您在此方法中获得的集合touches 中的对象是UITouches,因此它们具有view 属性,即最初发生触摸的视图。
  • 我试过这段代码,它正在处理没有任何动画的图像。但在我的场景中,我沿着贝塞尔路径移动 UIImageview,直到动画停止移动才会发生
【解决方案2】:

所以您希望用户能够沿着弯曲路径触摸关键帧动画中间的图像,然后将其拖动到不同的位置?你想在那个时候对动画发生什么?

你面临着多重挑战。

首先是在关键帧动画“进行中”时检测对象上的触摸。

为此,您需要使用父视图层的表示层的 hitTest 方法。

图层的表示图层表示图层在任何给定时刻的状态,包括动画。

一旦检测到对视图的触摸,您将需要从表示层获取图像的当前位置,停止动画,并使用基于 touchesMoved/touchesDragged 的​​动画来接管。

我编写了一个演示应用程序,展示了如何检测沿路径动画对象的触摸。这将是一个很好的起点。

看这里:

Core Animation demo including detecting touches on a view while an animation is "in flight".

【讨论】:

    【解决方案3】:

    最简单的方法是继承UIImageView

    对于简单的拖动,看看这里的代码(代码从用户 MHC 借来的):

    UIView drag (image and text)

    由于您要沿贝塞尔路径拖动,您必须修改touchesMoved:

     -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
            UITouch *aTouch = [touches anyObject];
    
            //here you have location of user's finger
            CGPoint location = [aTouch locationInView:self.superview];
    
            [UIView beginAnimations:@"Dragging A DraggableView" context:nil];
    
            //commented code would simply move the view to that point  
            //self.frame = CGRectMake(location.x-offset.x,location.y-offset.y,self.frame.size.width, self.frame.size.height);
    
            //you need some kind of a function
            CGPoint calculatedPosition = [self calculatePositonForPoint: location];
    
            self.frame = CGRectMake(calculatedPosition.x,calculatedPosition.y,self.frame.size.width, self.frame.size.height);
    
            [UIView commitAnimations];
        }
    

    您究竟想在-(CGPoint) calculatePositionForPoint:(CGPoint)location 中做什么 你决定。例如,您可以计算贝塞尔路径中最接近location 的点。对于简单的测试,您可以这样做:

    -(CGPoint) calculatePositionForPoint:(CGPoint)location {
    
        return location;
    }
    

    在此过程中,您将不得不决定如果用户感到惊讶远离您的 预先计算的贝塞尔路径。

    【讨论】:

      猜你喜欢
      • 2019-11-13
      • 2011-10-02
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多