通过触摸控制动画

时间:2010-12-28 12:39:43

标签: objective-c iphone core-animation

我有这种类型的代码......

    // create a UIImageView
UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rollDiceAnimationImage1.png"]];

// position and size the UIImageView
rollDiceImageMainTemp.frame = CGRectMake(0, 0, 100, 100);

// create an array of images that will represent your animation (in this case the array contains 2 images but you will want more)
NSArray *savingHighScoreAnimationImages = [NSArray arrayWithObjects:
                                                [UIImage imageNamed:@"rollDiceAnimationImage1.png"], 
                                                [UIImage imageNamed:@"rollDiceAnimationImage2.png"], 
                                                nil];

// set the new UIImageView to a property in your view controller
self.viewController.rollDiceImage = rollDiceImageMainTemp;

// release the UIImageView that you created with alloc and init to avoid memory leak
[rollDiceImageMainTemp release];

// set the animation images and duration, and repeat count on your UIImageView
[self.viewController.rollDiceImageMain setAnimationImages:savingHighScoreAnimationImages];
[self.viewController.rollDiceImageMain setAnimationDuration:2.0];
[self.viewController.rollDiceImageMain.animationRepeatCount:3];

// start the animation
[self.viewController.rollDiceImageMain startAnimating];

// show the new UIImageView
[self.viewController.view addSubview:self.rollDiceImageMain];

而不是startAnimation直接..有什么方法可以使用touchesMoved来控制这段代码?

2 个答案:

答案 0 :(得分:0)

您可以使用- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event之类的UIResponder方法检测触摸,因为您可以调用[self.viewController.rollDiceImageMain startAnimating];方法。

一旦它开始,那么你可以在一段时间后停止并限制触摸

此致

萨蒂亚

答案 1 :(得分:0)

实际上,你想要的是- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event。下面是一个代码示例,它允许UIView仅在x轴上移动。适应您需要的任何代码。

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    CGPoint original = self.center;
    UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.superview];


    // Taking the delta will give us a nice, smooth movement that will 
    // track with the touch.
    float delta =  location.x - original.x;

    // We need to substract half of the width of the view 
    // because we are using the view's center to reposition it.
    float maxPos = self.superview.bounds.size.width 
                   - (self.frame.size.width * 0.5f);
    float minPos = self.frame.size.width * 0.5f;   

    float intendedPos = delta + original.x;

    // Make sure they can't move the view off-screen
    if (intendedPos > maxPos)
    {
        intendedPos = maxPos;
    }

    // Make sure they can't move the view off-screen        
    if (intendedPos < minPos)
    {
        intendedPos = minPos;
    }

    self.center = CGPointMake(intendedPos, original.y);


    // We want to cancel all other touches for the view 
    // because we don't want the touchInside event firing.
    [self touchesCancelled:touches withEvent:event];

    // Pass on the touches to the super
    [super touchesMoved:touches withEvent:event];
}

请注意,我正在使用手指的增量和。如果你用手指跟踪,你会得到非常不稳定的行为,这是非常不受欢迎的。应用三角形将提供视图的良好,流畅的移动,将与触摸输入完美地跟踪。

更新:此外,对于那些想知道为什么我选择乘以0.5f而不是除以2的人来说,ARM处理器不支持硬件划分,因此通过乘法来实现微小的性能提升。这种性能优化可能只会在程序生命周期中被调用几次,因此它可能没有区别,但拖动时会多次调用此特定消息。因为在这种情况下,相反可能值得乘法。