【问题标题】:frame animation of uiimageView reduceduiimageView的帧动画减少
【发布时间】:2011-08-13 12:22:32
【问题描述】:

大家好,这是我的代码:

- (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。我不知道为什么。请问我该如何解决?对不起我的英语我是法国人:/

【问题讨论】:

    标签: iphone xcode animation uiimageview frame


    【解决方案1】:

    我不完全清楚您要达到的目标。似乎每次加速度计触发时,您都会添加一个新的 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;,而不是从视图中添加和删除,这样它就不会被绘制,并且具有相同的效果。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多