Showing posts with label iphone applicatio. Show all posts
Showing posts with label iphone applicatio. Show all posts

Sunday, June 7, 2015

Performing Segue and Passing data in iOS

I was trying to learn how to perform view switching using Segue for UI created by StoryBoard. Using Segue is quite easy and convenient. You can create Segue from Interface Builder, which associate action to view. Like if you click some button then you can associate this click action with view switch and you can select which view to switch to. This all is quite simple and can be done using simple drag and drop.

In case you want to perform view switch manually without any user interaction, you can call "performSegueWithIdentifier", like below. Here once my game is finished, I am calling "GameOverSegue", which will switch view to GameOver view.
self.performSegueWithIdentifier("GameOverSegue", sender: self)
If you want to pass some data while performing Segue, your calling ViewController needs to override "prepareForSegue" function. In this function you can set data to called view. Like below, I want to pass score to my GameOver view.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if( segue.identifier == "GameOverSegue") {
        let gameOverVC: GameOverViewController = 
            segue.destinationViewController as! GameOverViewController;
        gameOverVC.setScore(self.score)
    }
}
Thanks, hope this will be helpful.

Sunday, January 30, 2011

Simple animation with iPhone SDK

Recently I was trying port my application on iPhone and want to convert my Qt Animation code to iPhone SDK.

I achieved animation using core animation toolkit provided by iPhone SDK. In iPhone animation is performed using UIView object. You can animate changes to view property like frame, bounds, alpha, center, etc....

On iOS 4 and later animation can be done using block objects but if you want to run your code on iOS 3.2 or earlier than you have to use older begin/commit method.

I used begin/commit method to perform animation in my code.

Following snippet shows sample code that I used to animate a button's center to certain location.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];

// Make the animatable changes.
btn1.center = CGPointMake(150, btn1.center.y);

// Commit the changes and perform the animation.
[UIView commitAnimations];

Same code can also be written using new block object, like following.

[UIView animateWithDuration:1.0 animations:^{
btn1.center = CGPointMake(150, btn1.center.y);
}];

Tuesday, July 27, 2010

Detecting Long press( Tap and hold) on iPhone application

I was creating one game for iPhone and I required to detect Tap and Hold (Long press ) gesture in application. After trying a bit and reading some documentation, I found following solution. Solution is to perform Selector with some delay, and cancel Selector if touch has ended before enough time is passed.

Following is code from my application.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch began");
[self performSelector:@selector(longTap) withObject:nil afterDelay:1.0];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touch ended");
[NSObject cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(longTap) object:nil];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches moved");
[NSObject cancelPreviousPerformRequestsWithTarget:self 
selector:@selector(longTap) object:nil];
}

-(void) longTap 
{
NSLog(@"handle long tap..");
}

Hope this helps...