在视图中随机生成动画

时间:2012-06-07 22:58:19

标签: objective-c ios xcode animation

你好,我是动画的新手,最近开始玩它。我希望显示一个从小戒指到大戒指的圆圈(戒指),然后重新开始,除了在不同的位置之外再重复这个过程。

为此,我创建了6个相同戒指的不同图像尺寸。下面的代码是我如何做到但我不知道如何重复该过程(AkA它运行方法,但环的位置保持不变,我需要循环随机数生成器,但不知道何时检查图像是否已停止)。如果这不是正确的方法,请告诉我!附:当它达到最大的阶段时,戒指看起来非常难看,不知道为什么。

由于

-(void) doWork
{

NSInteger number;
NSInteger number2;
NSLog (@"here");

number = (arc4random()%100)+1; 
number2 = (arc4random()%100)+1; 
 NSLog (@" hello numer1: %d", number);
 NSLog (@" hello numer2: %d", number2);

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(number, number2, 135, 135)]; 

imageView.animationImages= [NSArray arrayWithObjects:
                            [UIImage imageNamed:@"smallestRing.png"],
                            [UIImage imageNamed:@"smallRing.png"], 
                            [UIImage imageNamed:@"mediumRing.png"],
                            [UIImage imageNamed:@"large2Ring.png"],
                            [UIImage imageNamed:@"largeRing.png"], nil]; 

imageView.animationDuration= 2.5;
imageView.animationRepeatCount= 0;
imageView.contentMode = UIViewContentModeScaleAspectFit;

[imageView startAnimating];
[self.view addSubview:imageView];

/*[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(doWork) userInfo:nil repeats:YES];*/

}

2 个答案:

答案 0 :(得分:1)

如果调整大小(或其他转换,如旋转或转换)是你需要的唯一动画,你可以使用方法+ animateWithDuration(你可以找到描述here

它看起来像:

-(void) doWork
{

NSInteger number;
NSInteger number2;
NSLog (@"here");

number = (arc4random()%100)+1; 
number2 = (arc4random()%100)+1; 
 NSLog (@" hello numer1: %d", number);
 NSLog (@" hello numer2: %d", number2);

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(number, number2, 135, 135)]; 

imageView.image = [UIImage imageNamed:@"largeRing.png"];
[self.view addSubview:imageView];

[self animate:imageView enlarge:YES];
}

-(void) animate:(UIImageView*)imageView enlarge:(BOOL)flag
{
if (flag) 
{
// then animating is starting
// and imageView will appear in random point of view
    int randomX = arc4random()%self.view.frame.size.width;
    int randomY = arc4random()%self.view.frame.size.height;
    imageView.center = CGPointMake(randomX, randomY);
}

double scale = flag ? 2 : 0.5;
[UIView animateWithDuration:5
         animations:^{ imageView.transform = CGAffineTransformMakeScale(scale,scale); }
         completion:^(BOOL finished){ [self animate:imageView: enlarge:!flag]; }];
}

答案 1 :(得分:1)

您可以查询bool isAnimating以确定UIImageView的动画是否已结束

P.S: 可能因为

而看起来很难看
imageView.contentMode = UIViewContentModeScaleAspectFit;

尝试类似

的内容
imageView.contentMode = UIViewContentModeCenter;