开发者

Control the animation by touch

i have this type of code....

    // 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 re开发者_开发知识库lease];

// 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];

Instead of startAnimation directly..there any way to control this code using touchesMoved??


Actually, what you want is the - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event. Below is a sample of code that will allow a UIView to move on the x-axis only. Adapt to whatever you need your code to do.

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

Notice here that I am taking the delta of the movement and not with the finger. If you track with the finger you will get very erratic behavior that is very undesirable. Applying the delta will give nice, fluid movement of the view that will track perfectly with the touch input.

Update: Also, for those wondering why I chose to multiply by 0.5f rather than divide by 2, the ARM processor doesn't support division in hardware, so there is a minuscule performance bump by going with multiplication. This performance optimization may only get called only a few times during the life of a program so it might not make a difference, but this particular message is called many, many times when dragging. Because it is in this case, it might be worth the multiplication, instead.


You can detect touches by using UIResponder methods like - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event in that you can call the [self.viewController.rollDiceImageMain startAnimating]; method.

And once it starts then you can stop after some time and restrict the touches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜