开发者

frame animation of uiimageView reduced

开发者 https://www.devze.com 2023-03-27 17:07 出处:网络
hie everyone, here is my code: - (void)viewDidLoad { [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/60.0];

hie everyone, here is my code:

- (void)viewDidLoad {

    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/60.0];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    [super viewDidLoad];
}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    valueX = acceleration.x *20.0;
    valueY = acceleration.y *-20.0;

    int newX = (int)(imageView.center.x+valueX);
    int newY = (int)(imageView.center.y+valueY);

    CGPoint newCenter = CGPointMake(newX,newY);
    imageView.center=newCenter;

    UIImageView* imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];
    imageView2.frame = CGRectMake(newX-12, newY-12, 24, 24);
    [self.view addSubview:imageView2];
    [UIView beginAnimations:nil context:imageView2];
    [UIView setAnimationDuration:10.5];
    [UIView setAnimationCurve:UIViewAnimationCu开发者_如何学GorveEaseOut];
    imageView2.frame = CGRectMake(newX-2, newY-2, 4, 4);
    [imageView2 setAlpha:0.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(removeSmoke:finished:context:)];
    [UIView commitAnimations];  
    if(imageView2.alpha=0){
        [imageView2 removeFromSuperview];
    }


    [self.view bringSubviewToFront:imageView];

}

At first my animation is very smooth with 1.0/60.0 but after some seconds the animation bug and is not smooth, i think it become 1.0/5.0. I don't know why . How can I solve this please ? sorry for my english I'm french :/


I'm not completely clear on what you are trying to achieve. It seems as though every time the accelerometer fires, you add in a new imageView animation, and want to move it based on some rules, as well shrink the image down. There are problems with your code though. You never call [imageView2 release];, and so you are probably getting memory problems after awhile.

You may also be doing too much computation in the method @selector(removeSmoke:finished:context:), but I'd need to see code in order to help with that.

If you know how many UIImageViews you are planning on having at one time, it is probably best to pre-load them, and put them in an array for easy access. That way, you can move one UIImageView, and move to the next one.

If you are shooting for iOS 4.0 or higher, I'd also recommend using Block Animations. They are easier to use, and I find a lot easier on the processor as well. Something like:

[UIView animateWithDuration:10.5
                     animations:^{ 
                       CGRect newRect = imageView2.frame;
                       newRect.origin.x -= newX-2;
                       newRect.origin.y -= newY-2;
                       imageView2.frame = newRect;
                     } 
                     completion:^(BOOL finished){

                       [UIView animateWithDuration:0.1
                                        animations:^{ 
                                          // This is where you complete your animation, maybe remove from view or something?
                                        } 
                                        completion:^(BOOL finished){
                                          ;
                                        }];
                     }];

Finally, instead of adding and removing from the view, I would suggest you just setting the imageView2.hidden = YES;, and that way it won't get drawn, and has the same effect. Hope that helps!

0

精彩评论

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

关注公众号