Showing posts with label objective c code examples. Show all posts
Showing posts with label objective c code examples. Show all posts

Saturday, 19 May 2012

App Level Notification for Iphone SDK

AppLevelNotification:




AppDelegate.h:----
#import

@class Animations2ViewController;

@interface Animations2AppDelegate : NSObject {
UIWindow *window;
Animations2ViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet Animations2ViewController *viewController;
-(void)appdelegateNotificationRecieved;
@end
AppDelegate.m:----
#import "Animations2AppDelegate.h"
#import "Animations2ViewController.h"

@implementation Animations2AppDelegate

@synthesize window;
@synthesize viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[[NSNotificationCenter defaultCenter]addObserver:viewController selector:@selector(notificationRecieved) name:@"maheshWokeUp" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appdelegateNotificationRecieved) name:@"maheshWokeUp" object:nil];
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];

return YES;
}
-(void)appdelegateNotificationRecieved{

NSLog(@"Appdelegate notificationr received");
}

- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}


@end


Animations2ViewController.h:----

#import

@interface Animations2ViewController : UIViewController {

IBOutlet UIImageView *imgView;
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
-(IBAction)startAnimating;
-(IBAction)stopAnimating;
-(void)notificationRecieved;
@end


Animations2ViewController.m:----

#import "Animations2ViewController.h"

@implementation Animations2ViewController

-(void)notificationRecieved{

NSLog(@"notification recieved");
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

NSLog(@"device moved");
}

-(IBAction)startAnimating{

[imgView startAnimating];
}
-(IBAction)stopAnimating{

[[NSNotificationCenter defaultCenter]postNotificationName:@"maheshWokeUp" object:nil];

}


- (void)viewDidLoad {
[super viewDidLoad];


UIImage *img1= [UIImage imageNamed:@"1.jpg"];
UIImage *img2= [UIImage imageNamed:@"2-1.jpg"];
UIImage *img3= [UIImage imageNamed:@"2.jpg"];
UIImage *img4= [UIImage imageNamed:@"3.jpg"];

NSArray *arrImages =[[NSArray alloc]initWithObjects:img1,img2,img3,img4,nil];
imgView.animationImages =arrImages;
imgView.animationDuration =10;
}

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
}


- (void)dealloc {
[super dealloc];
}

@end



Objective C Program For Animations In IPHONE

Animations program:


AppDelegate.h:----
#import <UIKit/UIKit.h>

@class Animations2ViewController;

@interface Animations2AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    Animations2ViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet Animations2ViewController *viewController;
@end
AppDelegate.m:----
#import "Animations2AppDelegate.h"
#import "Animations2ViewController.h"

@implementation Animations2AppDelegate

@synthesize window;
@synthesize viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
     // Override point for customization after app launch   
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
   
    return YES;
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end





Animations2ViewController.h:----

#import <UIKit/UIKit.h>

@interface Animations2ViewController : UIViewController<UIAccelerometerDelegate> {

    IBOutlet UIImageView *imgView;
    IBOutlet UIButton *btn1;
    IBOutlet UIButton *btn2;
}
-(IBAction)startAnimating;
-(IBAction)stopAnimating;
@end


Animations2ViewController.m:----

#import "Animations2ViewController.h"

@implementation Animations2ViewController

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

    NSLog(@"device moved");
}

-(IBAction)startAnimating{

    [imgView startAnimating];
}
-(IBAction)stopAnimating{

    [UIView beginAnimations:@"animation1" context:nil];
    [UIView  setAnimationCurve:UIViewAnimationCurveLinear];

    [UIView setAnimationDuration:10];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:btn2 cache:YES];
    btn1.frame =CGRectMake(100, 300, 60, 40);
    [UIView commitAnimations];

    }


- (void)viewDidLoad {
    [super viewDidLoad];
   
   
    UIImage *img1= [UIImage imageNamed:@"1.jpg"];
    UIImage *img2= [UIImage imageNamed:@"2-1.jpg"];
    UIImage *img3= [UIImage imageNamed:@"2.jpg"];
    UIImage *img4= [UIImage imageNamed:@"3.jpg"];
   
    NSArray *arrImages =[[NSArray alloc]initWithObjects:img1,img2,img3,img4,nil];
    imgView.animationImages =arrImages;
    imgView.animationDuration =10;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    }


- (void)dealloc {
    [super dealloc];
}

@end