Hi Guyz, in this section i will give you the idea of moving your objects(Image in our case) in the screen using Touch function. In previous blog, we talked about the drag and drop method, where we were dragging some object from one position and dropping it to another one but in this section we are only going to reposition/move the object in the screen.So basically in this section, we will see how the multiple images moved using touch function. Lets get started !!!!
Create multiple (say 4 in our case) UIImageView in your viewcontroller and connect it to the IBOutlet and assign them with different images.
In your .h file ;
@property(nonatomic,retain) IBOutlet UIImageView *imageView1;
@property(nonatomic,retain) IBOutlet UIImageView *imageView2;
@property(nonatomic,retain) IBOutlet UIImageView *imageView3;
@property(nonatomic,retain) IBOutlet UIImageView *imageView4;
In your .m file,
@synthesize imageView1,imageView2,imageView3,imageView4;
Now,use Touch delegate method as :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == imageView1) { // If touched view is imageView1 , then assign it its new location
imageView1.center = touchLocation;
}
else if ([touch view] == imageView2) {
imageView2.center = touchLocation;
}
else if ([touch view] == imageView3) {
imageView3.center = touchLocation;
}
else if ([touch view] == imageView4) {
imageView4.center = touchLocation;
}
}
So that's it!! so simple to move objects around the screen in iOS apps.
Create multiple (say 4 in our case) UIImageView in your viewcontroller and connect it to the IBOutlet and assign them with different images.
In your .h file ;
@property(nonatomic,retain) IBOutlet UIImageView *imageView1;
@property(nonatomic,retain) IBOutlet UIImageView *imageView2;
@property(nonatomic,retain) IBOutlet UIImageView *imageView3;
@property(nonatomic,retain) IBOutlet UIImageView *imageView4;
In your .m file,
@synthesize imageView1,imageView2,imageView3,imageView4;
Now,use Touch delegate method as :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == imageView1) { // If touched view is imageView1 , then assign it its new location
imageView1.center = touchLocation;
}
else if ([touch view] == imageView2) {
imageView2.center = touchLocation;
}
else if ([touch view] == imageView3) {
imageView3.center = touchLocation;
}
else if ([touch view] == imageView4) {
imageView4.center = touchLocation;
}
}
So that's it!! so simple to move objects around the screen in iOS apps.
Hi, Thank you so much for the code, I'm lerning to code in objective-c, I'm using your code, but, the if/else sentences are not working for me, I'm using XCode 5 with iOS7, maybe this is the problem? What can I do to manage several images?
ReplyDeleteThank you.
Hi sir,
ReplyDeletei am using this code but it not working .
Thanks