Анимация снегопада + эффекты ветра

Я хочу реализовать анимацию снегопада, и пользователи могут нажать кнопку, чтобы добавить эффекты ветра к падающему снегу. У меня есть следующие коды, которые могут заставить снег падать, но при добавлении эффектов ветра, следующие снега замедляются, без эффекта ветра.

SnowFallView, который имеет методы для создания эффекта снега и ветра. Есть три ключевые функции:

-(void)createFlakes {   
srandomdev();
self.flakesArray = [[NSMutableArray alloc] initWithCapacity:self.flakesCount];

for (int i=0; i < self.flakesCount; i++) {
    // generate image

            NSString *imageFileName = @"snow.png";

    UIImage *flakeImg = [UIImage imageNamed:imageFileName];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:flakeImg];
    imageView.userInteractionEnabled = NO;

    [self.flakesArray addObject:imageView];
    [self addSubview:imageView];
}
}

-(void) startToSnow {
static BOOL isFirst = YES;
int ctr = 1;

if (!self.flakesArray)
    [self createFlakes];
self.backgroundColor = [UIColor clearColor];


CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];
theAnimation.repeatCount = 1e100;
theAnimation.autoreverses = NO;


for (UIImageView *v in self.flakesArray) {
    CGPoint p;
    p.x = (Float32)(rand()%(int)self.frame.size.width);
    p.y = self.frame.size.height+v.frame.size.height*(ctr<4?1:isFirst?(rand()%20):(rand()%20+5));
    CGPoint startPoint;
    startPoint.x = p.x;
    startPoint.y = -p.y;

    float timeInterval = ((Float32)(self.animationDurationMax-self.animationDurationMin)*(Float32)random()/(Float32)RAND_MAX);
    theAnimation.duration = timeInterval+self.animationDurationMin;
    theAnimation.fromValue = [NSValue valueWithCGPoint:startPoint];
    CGPoint endPoint;
    endPoint.y = self.frame.size.height;
    endPoint.x = p.x;
    if (!endPointArray) {
        endPointArray = [[NSMutableArray alloc] init];
    }
    [endPointArray addObject:[NSValue valueWithCGPoint:endPoint]];

    theAnimation.toValue = [NSValue valueWithCGPoint:endPoint];
    float spd = (v.frame.size.height/(51.0-(Float32)(rand()%7))*kFallSpeed)/6.0;
    theAnimation.speed = (rand()%2)?spd:spd*1.5;

    if (!animationArray) {
        animationArray = [[NSMutableArray alloc] init];
    }
    [animationArray addObject:[theAnimation copy]];


    [v.layer addAnimation:theAnimation forKey:@"transform.translation"];

}

isFirst = NO;
}

-(void)windLeft:(BOOL)left
{

CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];
theAnimation.repeatCount = 1e100;
theAnimation.autoreverses = NO;

NSInteger index = 0;
for (UIImageView *v in self.flakesArray) {



    CGPoint startPoint;
    CGRect currentFrame = [v.layer.presentationLayer frame];
    startPoint.x = currentFrame.origin.x;
    startPoint.y = currentFrame.origin.y;
    NSLog(@"x:%f,y:%f",startPoint.x, startPoint.y);
    CABasicAnimation *previousAnimation = [animationArray objectAtIndex:index];

    theAnimation.duration = previousAnimation.duration;
    theAnimation.fromValue = [NSValue valueWithCGPoint:startPoint];
    CGPoint endPoint = [[endPointArray objectAtIndex:index] CGPointValue];
    CGPoint newEndPoint;
    newEndPoint.y = endPoint.y;
    if (left)
        newEndPoint.x = endPoint.x - 100;
    else
        newEndPoint.x = endPoint.x + 100;

    theAnimation.toValue = [NSValue valueWithCGPoint:endPoint];
    theAnimation.speed = previousAnimation.speed;
    [animationArray replaceObjectAtIndex:index withObject:[theAnimation copy]];
    [v.layer removeAnimationForKey:@"transform.translation"];
    [v.layer addAnimation:theAnimation forKey:@"transform.translation"];

    index++;
}



}

На главном экране я использую следующий код для вызова

IBOutlet UIView *snowFallView; //for snow flake view
IBOutlet UIButton *windButton;
SnowFallView *sfv;

В методе viewDidLoad

sfv = [[SnowFallView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
sfv.flakesCount = 20;
[snowFallView addSubview:sfv];
[snowFallView addSubview:windButton];
[sfv startToSnow];

Когда пользователи нажимают кнопку, чтобы добавить эффекты ветра

- (IBAction)pushWind: (id)sender
{
    [sfv windLeft:YES];
}

0 ответов

Другие вопросы по тегам