• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips
You are here: Home / ios / Generate random numbers – Example of motion and touch event

Generate random numbers – Example of motion and touch event

April 29, 2014 By Ravi Shankar Leave a Comment

Let us see how to use motion and touch event in iOS by looking through a simple project that displays random number. This project displays random number on a label for a touch and motion event. Check out this link for more about events in iOS

201404291450.jpg

After creating a single view application, add a new class that contains the method for generating random. If you do not want to add new class just for a single method then you can add the method as part of your ViewController class itself. arc4random_uniform accepts a seed value (90) and generated number will be below this seed value.

-(NSInteger)generateNumber {

   return arc4random_uniform(90);

}

Now add a UILabel for displaying the generated random numbers. This includes both adding IBOutlet property and connecting it with UILabel on Interface Builder.


Motion Event

The are three motion-handling methods -(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event, -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event and -(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

In this example, we can use the motionEnded method to call the displayNumber method

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

if (UIEventSubtypeMotionShake) {

[self displayNumber];

}

}

-(void)displayNumber {

RandomNumber *randomNumber = [[RandomNumber alloc] init];

self.displayRandomNumber.text = [NSString stringWithFormat:@”%d”,[randomNumber generateNumber]];

}

And to test this on Simulator, use the Shake Gesture menu under Hardware to simulate motion event.

201404291717.jpg

Touch Event

There are four touch events corresponding to each touch phase.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

In this example, we will use the touchesEnded method for displaying the random numbers.

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

[self displayNumber];

}

You can download the source code along with TDD classes for this beginner tutorial on touch and motion event from here.

Filed Under: ios, Programming, Xcode Tagged With: motion events, touch events, Xcode

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy