Skip to content

Instantly share code, notes, and snippets.

@parthjdabhi
Created July 9, 2016 13:19
Show Gist options
  • Save parthjdabhi/dc2c15e4ca585f67aaa8e0e11cbc43bb to your computer and use it in GitHub Desktop.
Save parthjdabhi/dc2c15e4ca585f67aaa8e0e11cbc43bb to your computer and use it in GitHub Desktop.
//
// FallingStuffAnimationVC.m
// UIGravityBehavior
//
// Created by Mark Parth Dabhi on 06/07/2016.
// Copyright (c) 2014 Parth Dabhi. All rights reserved.
//
#import "FallingStuffAnimationVC.h"
#define DegreesToRadians(x) ((x) * M_PI / 180.0)
#define LEAF_DROP_INTERVAL 0.25
#define NUMBER_OF_LEAF_TYPES 7
#define DRIFT_X_VARIANCE 40
#define ROTATION_VARIANCE 90
#define MEAN_FALL_TIME 10
#define FALL_TIME_VARIANCE 5
@interface FallingStuffAnimationVC ()
@property (nonatomic, strong) NSTimer *FallingTimer;
@end
@implementation FallingStuffAnimationVC
-(void) animateLeaves:(id) sender {
int piecenum = (arc4random()%NUMBER_OF_LEAF_TYPES) + 1;
int screenWidth = self.view.frame.size.width;
int screenHeight = self.view.frame.size.height+100;
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"heart%d", piecenum]];
UIImageView *newPiece = [[UIImageView alloc] initWithImage:image];
int startX = arc4random()%screenWidth;
int endX = startX + ((arc4random()%DRIFT_X_VARIANCE) - (DRIFT_X_VARIANCE / 2));
newPiece.center = CGPointMake(startX, -image.size.height);
[self.view insertSubview:newPiece atIndex:0];
int time = ((arc4random()%FALL_TIME_VARIANCE) + (MEAN_FALL_TIME - (FALL_TIME_VARIANCE / 2)));
[UIView animateWithDuration:time
animations:^(void) {
float rotation =
DegreesToRadians((arc4random()%ROTATION_VARIANCE) - (ROTATION_VARIANCE / 2));
newPiece.transform = CGAffineTransformMakeRotation(rotation);
newPiece.center = CGPointMake(endX, screenHeight + image.size.height);
}completion:^(BOOL finished) {
[newPiece removeFromSuperview];
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.FallingTimer = [NSTimer scheduledTimerWithTimeInterval:LEAF_DROP_INTERVAL
target:self
selector:@selector(animateLeaves:)
userInfo:nil
repeats:TRUE];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment