uiimageView 的帧动画减少

发布于 2024-11-29 07:21:46 字数 1406 浏览 1 评论 0原文

大家好,这是我的代码:

- (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:UIViewAnimationCurveEaseOut];
    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];

}

起初我的动画在 1.0/60.0 下非常流畅,但几秒钟后动画出现错误并且不流畅,我认为它变成了 1.0/5.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:UIViewAnimationCurveEaseOut];
    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 :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一片旧的回忆 2024-12-06 07:21:46

我不完全清楚你想要实现什么目标。似乎每次加速计触发时,您都会添加一个新的 imageView 动画,并希望根据某些规则移动它,并缩小图像。不过您的代码存在问题。您从不调用 [imageView2 release];,因此一段时间后您可能会遇到内存问题。

您还可能在方法 @selector(removeSmoke:finished:context:) 中进行了太多计算,但我需要查看代码才能帮助解决此问题。

如果您知道计划一次拥有多少个 UIImageView,那么最好预先加载它们,并将它们放入一个数组中以便于访问。这样,您可以移动一个 UIImageView,然后移动到下一个。

如果您使用 iOS 4.0 或更高版本进行拍摄,我还建议使用块动画。它们更容易使用,而且我发现处理器也更容易使用。最后

[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){
                                          ;
                                        }];
                     }];

,我建议您只设置 imageView2.hidden = YES; ,而不是从视图中添加和删除,这样它就不会被绘制,并且具有相同的效果。希望有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文