سؤال

I'd like to do

  1. change animationDuration to 0
  2. do something
  3. change animationDuration to something longer (1.0f for example)
  4. do something else

... all in the touchesBegan method without any "pauses" in between. But it seems that it wont let me do that.

like this:

s1.animationDuration = 0.0f;
s1.center = touchedPoint;
s1.alpha = 1.0f;
s1.animationDuration = 1.0f;
s1.alpha = 0.0f;

full example here: https://gist.github.com/gregtemp/5086240

I know i could just move it to the touchesEnded method but I want to avoid doing that.

هل كانت مفيدة؟

المحلول

In your question you're asking how to:

  1. update the properties of an object
  2. move it
  3. update the properties of the same object
  4. fade it out

... so that it can reappear at another place when you touch the screen.

Also, you want to do this in a single method...

I would suggest taking a different approach to working out this problem.

First, try to think of shapes as objects that are persistent until you delete or dispose of them. Basically, you can treat the object as a thing that's going to get passed around to various methods.

When you start thinking like this, then you can use the following technique to make the effect you're looking for:

#import "C4WorkSpace.h"

@implementation C4WorkSpace 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {
        CGPoint touchPoint = [t locationInView:self.canvas];
        [self createObjectAtPoint:touchPoint];
    }
}

-(void)createObjectAtPoint:(CGPoint)newPoint {
    C4Shape *s = [C4Shape ellipse:CGRectMake(newPoint.x-25,newPoint.y-25,50,50)];
    s.userInteractionEnabled = NO;
    [self.canvas addShape:s];
    [self runMethod:@"fadeAndRemoveShape:" withObject:s afterDelay:0.0f];
}

-(void)fadeAndRemoveShape:(C4Shape *)shape {
    shape.animationDuration = 1.0f;
    shape.alpha = 0.0f;
    [shape runMethod:@"removeFromSuperview" afterDelay:shape.animationDuration];
}

@end

What this does is:

  1. gets a touch point
  2. passes the touch point to a method that creates a shape
  3. passes the created shape to a method that fades it out
  4. removes the shape from the canvas when it has disappeared
  5. the shape is automatically removed from memory after being removed from the screen
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top