Skip to content

Instantly share code, notes, and snippets.

@AlexanderBollbach
Created August 17, 2015 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderBollbach/580035346526a2cb6e09 to your computer and use it in GitHub Desktop.
Save AlexanderBollbach/580035346526a2cb6e09 to your computer and use it in GitHub Desktop.
#import "ViewController.h"
typedef NS_ENUM(NSInteger, HDDirection) {
HDDirectionNorth = 0,
HDDirectionSouth = 1,
HDDirectionWest = 2,
HDDirectionEast = 3,
};
@interface ViewController ()
@property (nonatomic, assign) HDDirection currentDirection;
@end
@implementation ViewController {
float _yValue;
float _xValue;
}
- (CGPoint)getDirection{
if ((int)self.currentDirection == 4) {
self.currentDirection = 0;
}
self.currentDirection += 1;
CGPoint eastPoint = CGPointMake(1, 0);
CGPoint westPoint = CGPointMake(-1, 0);
CGPoint northPoint = CGPointMake(0, 1);
CGPoint southPoint = CGPointMake(0, -1);
CGPoint noPoint = CGPointMake(0, 0);
switch (self.currentDirection) {
case HDDirectionEast:
return eastPoint;
case HDDirectionWest:
return westPoint;
case HDDirectionSouth:
return southPoint;
case HDDirectionNorth:
return northPoint;
default:
return noPoint;
}
}
-(void)changeDirection {
CGPoint point = [self getDirection];
_xValue = point.x;
_yValue = point.y;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_yValue = 0.0f;
_xValue = 0.0f;
self.orb1 = [[customView1 alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100, 100)];
self.orb1.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
self.orb1.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.orb1];
self.containerRect = CGRectInset(self.view.frame, 20, 20);
[NSTimer scheduledTimerWithTimeInterval:0.0009
target:self
selector:@selector(engine)
userInfo:nil
repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(changeDirection)
userInfo:nil
repeats:YES];
}
- (void)engine {
CGRect tempFrame = self.orb1.frame;
tempFrame.origin.x += _xValue;
tempFrame.origin.y += _yValue;
self.orb1.frame = tempFrame;
if (!CGRectContainsPoint(self.containerRect, self.orb1.center)) {
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment