Image change using touch function in iPhone

In this application we will see how to images change using touch function.

Step 1: Create a Window base application using template. Give the application name “MovingBall12”.

Step 2 : Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add UIView class i the project. Select Classes -> Add -> New File -> Cocoa Touch Class -> Objective C class -> select UIView from the Subclass of. Give the file name “BallView”.

Step 4: We have added two resources in the Resources folder. Give the name of the resource “1.png”,”2.png”.

Step 5: In the MovingBall12AppDeleagte.h file, we have added BallView class, NSTimer , so make the following changes in the file.

#import <UIKit/UIKit.h>

@class BallView;

@interface MovingBall12AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
        BallView *ballView;
        int count;
        NSTimer *ViewTimer;
       
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet BallView *ballView;
@property (nonatomic, retain) IBOutlet NSTimer *ViewTimer;

Step 6: Open the BallView.h file, we have added UIImageView, UILabel, NSTimer, so make the following chnages:

#import <UIKit/UIKit.h>

@interface BallView : UIView {
       
        NSTimer *ViewTimer;
        UIImageView *theBall;
        UILabel *touchMe;
        CGRect touchMeFrame;
        int count,switcher;
        int directionX, directionY;
        BOOL gameRunning;
        NSArray *images;
}

@property(nonatomic,retain) NSTimer *ViewTimer;
@property(nonatomic,retain) IBOutlet UIImageView *theBall;
@property(nonatomic,retain) IBOutlet UILabel *touchMe;
@property(nonatomic,retain) NSArray *images;

@property CGRect touchMeFrame;
@property int directionX, directionY;
@property BOOL gameRunning;

Step 7: Double click the MainWindow,xib file and open it to the Interface Builder. Double click the window icon from the main window and open it , first drag the View from the library and place it to the window. Next drag label and ImageView from the library and place it to the view window. Select the View from the main window and bring up Identity Inspector and change the class name into BallView. Connect BallView to the label and select touchMe,and connect BallView to the ImageView and select the theBall (See the figure below). Now save MainWindow.xib file, close it and go back to the Xcode.

Step 8: Open the BallView.m fie make the following changes in the file.

(void)onTimer {
        CGPoint center = theBall.center;
       
        float newX = center.x + directionX;
        float newY = center.y + directionY;
       
        int rndX = rand() % 3 + 1;
        int rndY = rand() % 3 + 1;
       
        int screenX = self.frame.origin.x + self.frame.size.width;
        int screenY = self.frame.origin.y + self.frame.size.height;
       
        BOOL changed = NO;
        if (newX < 0) {
                directionX = rndX;
                newX = 0;
                changed = YES;
        } else if (newX > screenX) {
                directionX = rndX;
                newX = screenX;
                changed = YES;
        }
       
        if (newY < 0) {
                directionY = rndY;
                newY = 0;
                changed = YES;
        } else if (newY > screenY) {
                directionY = rndY;
                newY = screenY;
                changed = YES;
        }
       
        if ((rand() % 1000) > 997) {
                directionY *= 1;
                changed = YES;
        }
       
        if ((rand() % 1000) > 997) {
                directionX *= 1;
                changed = YES;
        }
       
        if (changed == YES) {
                int img = switcher++ % (int)[images count];
                theBall.image = [images objectAtIndex:img];
               
        }
       
        CGPoint newCenter = CGPointMake(newX, newY);
        [theBall setCenter:newCenter];
       
}

(void)toggleTimer:(int)withCount {
        if (withCount % 2 != 0) {
                [ViewTimer invalidate];
                touchMe.backgroundColor = [UIColor whiteColor];
                touchMe.textColor = [UIColor blackColor];
               
                               
                gameRunning = NO;
               
        } else {
                ViewTimer = [NSTimer scheduledTimerWithTimeInterval:0.0075 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];  
                touchMe.backgroundColor = [UIColor blackColor];
                touchMe.textColor = [UIColor whiteColor];
               
                gameRunning = YES;
        }
}

(void)doInitialSetup {
        sranddev();
       
        count = 0;
       
        int rndX = rand() % 3 + 1;
        int rndY = rand() % 3 + 1;
       
        directionX = rndX;
        directionY = rndY;
       
        UIImage *img1 = [UIImage imageNamed:@"1.png"];
        UIImage *img2 = [UIImage imageNamed:@"2.png"];
       
        self.images = [NSArray arrayWithObjects:img1, img2, nil];
       
        int img = (int)count % (int)[images count];
        theBall.image = [images objectAtIndex:img];
       
        gameRunning = NO;
}

(BOOL) isPointInTouchMe:(CGPoint)point {
        return (point.x >= touchMe.frame.origin.x
                        && point.x <= touchMe.frame.origin.x + touchMe.frame.size.width
                        && point.y >= touchMe.frame.origin.y
                        && point.y <= touchMe.frame.origin.y + touchMe.frame.size.height);
}

(BOOL) isPointInTheBall:(CGPoint)point {
        return (point.x >= theBall.frame.origin.x
                        && point.x <= theBall.frame.origin.x + theBall.frame.size.width
                        && point.y >= theBall.frame.origin.y
                        && point.y <= theBall.frame.origin.y + theBall.frame.size.height);
}

(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self];
       
        if ([self isPointInTouchMe:point]) {
                [self toggleTimer:count++];
        }
       
}
(void) awakeFromNib {
        [self doInitialSetup];
}

(id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
                [self doInitialSetup];
    }
    return self;
}

(void)drawRect:(CGRect)rect {
        [super drawRect:rect];
}

Step 9: Now compile and run the application on the Simulator.

You can Download SourceCode from here MovingBall12

Leave a Reply

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