简体   繁体   中英

Animate an Image with CGAffineTransform and get CGPoint at specified point?

I am new in iPhone and I animate an Image with CGAffineTransform from top to bottom and i want to perform some task when Image at (155,50) position how to get this position by these code or other source ?

    CGRect frameRect=CGRectMake(155, 0, 9, 8);
boxView=[[UIImageView alloc] initWithFrame:frameRect];
boxView.image = [UIImage imageNamed:@"Projectile2.png"];
[self.view addSubview:boxView];
[boxView release];

CGPoint location = CGPointMake(159.5, 500);

[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDelegate:self];
[UIImageView setAnimationDuration:1.5f];
[UIImageView setAnimationCurve:UIViewAnimationCurveEaseInOut];

CGAffineTransform scaleTrans = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(angle * M_PI / 180);

boxView.transform = CGAffineTransformConcat(scaleTrans, rotateTrans);
angle = (angle == 180 ? 360 : 180);
boxView.center = location;
[UIImageView commitAnimations];

Thanks in Advance...

AFAIK there are no callbacks that will be called during the animation, just at the beginning or the end of the animation (this seems true both for block based animations and the old non block methods)

You can do something like this:

1- register a KVO observer for the "center" property of the view you want to track

[viewToTrack addObserver:anObserver 
              forKeyPath:@"center" 
                 options:NSKeyValueObservingOptionNew  
                 context:somePointerToYourContextOrNIL];

2- the callback will be called everytime the center property is changing. anObserver MUST implement this method

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context

keyPath will contain the name of the property you are tracking, object is the viewToTrack

change will contain the new value (based on the previous example) and context is

somePointerToYourContextOrNIL (it can be nil)

3- in the callback you will receive (based on how you register it) the old and/or the new value for the property

4- check when you are near enough to the point where you should do wgìhat you need to do

Here is a primer on KVO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM