Swipable Image View - ios

so I am trying to create a view were you can swipe through images left to right. I have gotten it able to pop up a blank page showing my title and recognizing the swipe gestures (left or right) and icnreasing the indexToShow as required. However, I am not able to see any of my images just a blank screen. Any help would be appreciated.
Here is what I have done:
in .h file
UIImageView *myImageView;
int indexToShow;
in .m file
#interface myController ()
#property (nonatomic, strong) NSMutableArray *imgArray;
#end
#implementation myController
#synthesize imgArray;
and then I have
- (void)viewDidLoad
{
[super viewDidLoad];
imgArray = [[NSMutableArray alloc] initWithObjects:#"image1.png",#"image2.png",#"image3.png",#"image4.png", nil];
indexToShow = 0;
UISwipeGestureRecognizer *gestureRight;
UISwipeGestureRecognizer *gestureLeft;
gestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRight:)];
gestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeLeft:)];
[gestureLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:gestureRight];
[[self view] addGestureRecognizer:gestureLeft];
myImageView.image = [imgArray objectAtIndex:indexToShow];
}
- (void)swipeRight:(UISwipeGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
if ((indexToShow-1) < -1) {
indexToShow = imgArray.count-1;
}
myImageView.image = [imgArray objectAtIndex:indexToShow];
indexToShow--;
}
}
- (void)swipeLeft:(UISwipeGestureRecognizer *)gesture
{
if ((gesture.state == UIGestureRecognizerStateChanged) ||
(gesture.state == UIGestureRecognizerStateEnded)) {
if ((indexToShow+1) > imgArray.count ) {
indexToShow = 0;
}
myImageView.image = [imgArray objectAtIndex:indexToShow];
indexToShow++;
}
}
and then in my viewwillappear I have
[super viewWillAppear:animated];
self.title = #"Hi";
[self.view addSubview:tutorialImageView];

I recommend that you use UICollectionView instead of doing this for efficiency purposes, but your current problem has a simple fix. When you created your array, you made it an array of strings (file names).
imgArray = [[NSMutableArray alloc] initWithObjects:#"image1.png",#"image2.png",#"image3.png",#"image4.png", nil];
But when you access it, you're taking this string (file name) and setting it as the image view's image.
myImageView.image = [imgArray objectAtIndex:indexToShow];
Instead, you should do as #Larme suggested and use the following
myImageView.image = [UIImage imageName:[imgArray objectAtIndex:indexToShow]];
This will create a UIImage from the file name at the selected index in the array, and set it as the image view's image.

Related

Why is my UIGestureRecognizer Swipe not working? Xcode

Here's my GameViewController.m file:
- (void)viewDidLoad {
[super viewLoad];
.
.
.
_board = [[TwinstonesBoardModel alloc] init];
[_board setToInitialStateMain];
TwinstonesStoneView* twinstonesBoard = [[TwinstonesStoneView alloc]
initWithMainFrame:CGRectMake(12, 160, 301.5, 302.5)
andBoard:_board];
[self.view addSubview:twinstonesBoard];
TwinstonesStonesView *stoneOne = [[TwinstonesStoneView alloc] init];
TwinstonesStonesView *one = (TwinstonesStoneView*)stoneOne.stoneUnoView;
TwinstonesStonesView *stoneTwo = [[TwinstonesStoneView alloc] init];
TwinstonesStonesView *two = (TwinstonesStoneView*)stoneTwo.stoneDueView;
UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(swipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.numberOfTouchesRequired = 1;
[one addGestureRecognizer:swipeLeft];
[two addGestureRecognizer:swipeLeft];
Here's the relevant code in my TwinstonesStoneView.m file:
#implementation TwinstonesStoneView
{
NSMutableArray* _array;
NSMutableArray* _emptyArray;
CGRect _frame;
NSUInteger _column;
NSUInteger _row;
TwinstonesBoardModel* _board;
int _i;
}
- (id)initWithMainFrame:(CGRect)frame andBoard:
(TwinstonesBoardModel*)board
{
if (Self = [super initWithFrame:frame])
{
float rowHeight = 49.0;
float columnWidth = 49.0;
float barrierHorizontalRowHeight = 12.5;
float barrierVerticalColumnWidth = 12.5;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
TwinstonesStonesView* square = [[TwinstonesStoneView alloc]
initWithEmptyFrame:CGRectFrame(//spacial equations, not important)
column:col
row:row
board:board];
BoardCellState state = [board cellStateAtColumn:col andRow:row];
if (state == BoardCellStateStoneOne) {
// _stoneUnoView is a public property
// 'stoneOneCreation' creates a UIImageView of the stone
_stoneUnoView = [UIImageView stoneOneCreation];
[self addSubview:square];
[square addSubview:_stoneUnoView];
[_array insertObject:_stoneUnoView atIndex:0];
} else if (state == BoardCellStateStoneTwo) {
// same idea as above
_stoneDueView = [UIImageView stoneTwoCreation];
[self addSubview:square];
[square addSubview:_stoneDueView];
[_array insertObject:_stoneDueView atIndex:1];
} else {
// based on the 'init' method I write below, I assumed this
// would return an empty square cell
[self addSubview:square];
[_emptyArray insertObject:square atIndex:_i];
_i++;
}
}
}
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (UIView*)stoneUnoView {
return _stoneUnoView;
}
- (UIView*)stoneDueView {
return _stoneDueView;
}
- (id)initWithEmptyFrame:(CGRect)frame
column:(NSUInteger)column
row:(NSUInteger)row
board:(TwinstonesBoardModel*)board
{
self = [super initWithFrame:frame];
return self;
}
- (void)swipeLeft:(UIGestureRecognizer*)recognizer
{
NSLog(#"Swipe Left");
UIView* view = recognizer.view;
[self move:CGPointMake(-1, 0) withView:view];
}
- (void)move:(CGPoint)direction withView:view {
// whatever code I decide to put for stone movement
}
#end
I apologize for the (probably) unnecessary length, I've just trying to figure this out for a couple days and have had no luck. Here's the bullet points of what I'm trying to do:
1. setInititalStateMain sets the placements of two stones in a 5x5 grid
2. In GameViewController.m, I'm trying to capture the 'stoneUnoView' and
'stoneDueView' properties (set in the TwinstonesStoneView.m file), give them swipe gestures, and interact with them using the methods provided in TwinstonesStoneView.m.
3. Am I generating too many views? The catch is that everything works in terms of what I'm able to see on my IPhone when I run the program. The stones show up on my screen, but when I try to interact with them, not even the 'NSLog' message shows up in the console.
4. The 'stoneOneCreation' method (and ...two) are UIImageView's, but, as you can see, I store them in a UIView pointer.
5. I also used '[one setUserInteractionEnabled:YES]' (and ...two) but that didn't help either.
6. If I add the gesture recognizer to self.view, everything works (the displays of the stones, gameboard, and other graphics appears, and when I interact with ANY part of the screen, I output the directions to the console......just not stone-specific interaction).
Thank you so very much for putting up with all of this, this will really help if someone knows what's wrong. PS: all file #import's are correct, so that isn't a problem.
I am using XCode 7, Objective-C language, and developing for iOS
Anthony
One thing that catches my eye is I don't think you can add the same gesture recognizer to multiple views. I suspect, at best, only the last view is actually receiving it.
try this but i am not sure try this, just create 2 swipe gestures
in GameViewController.m
- (void)viewDidLoad {
[super viewLoad];
//.... other code
//comment below line
// UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc]
//initWithTarget:self //setting self is the problem is the problem
//action:#selector(swipeLeft:)];
UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:stoneOne //set target will be one
action:#selector(swipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.numberOfTouchesRequired = 1;
[one addGestureRecognizer:swipeLeft];
UISwipeGestureRecognizer* swipeLeft_2 = [[UISwipeGestureRecognizer alloc]
initWithTarget:stoneTwo //this will be two
action:#selector(swipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.numberOfTouchesRequired = 1;
[two addGestureRecognizer:swipeLeft_2]; //set the gesture
}
u are setting the gesture to self which means the actions are sent to GameViewController.m but we want actions to be in TwinstonesStoneView.m so change the target to view to TwinstonesStoneView. And also if it is image views u are adding gestures, then enable user interaction for each image views setUserInteractionEnabled:
just try it

UISwipeGesture Not working using custom UIView

Here's my GameViewController.m file:
- (void)viewDidLoad {
[super viewLoad];
.
.
.
_board = [[TwinstonesBoardModel alloc] init];
[_board setToInitialStateMain];
TwinstonesStoneView* twinstonesBoard = [[TwinstonesStoneView alloc]
initWithMainFrame:CGRectMake(12, 160, 301.5, 302.5)
andBoard:_board];
[self.view addSubview:twinstonesBoard];
TwinstonesStonesView *stoneOne = [[TwinstonesStoneView alloc] init];
TwinstonesStonesView *one = (TwinstonesStoneView*)stoneOne.stoneUnoView;
TwinstonesStonesView *stoneTwo = [[TwinstonesStoneView alloc] init];
TwinstonesStonesView *two = (TwinstonesStoneView*)stoneTwo.stoneDueView;
UISwipeGestureRecognizer* swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(swipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft.numberOfTouchesRequired = 1;
[one addGestureRecognizer:swipeLeft];
[two addGestureRecognizer:swipeLeft];
Here's the relevant code in my TwinstonesStoneView.m file:
#implementation TwinstonesStoneView
{
NSMutableArray* _array;
NSMutableArray* _emptyArray;
CGRect _frame;
NSUInteger _column;
NSUInteger _row;
TwinstonesBoardModel* _board;
int _i;
}
- (id)initWithMainFrame:(CGRect)frame andBoard:
(TwinstonesBoardModel*)board
{
if (Self = [super initWithFrame:frame])
{
float rowHeight = 49.0;
float columnWidth = 49.0;
float barrierHorizontalRowHeight = 12.5;
float barrierVerticalColumnWidth = 12.5;
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
TwinstonesStonesView* square = [[TwinstonesStoneView alloc]
initWithEmptyFrame:CGRectFrame(//spacial equations, not important)
column:col
row:row
board:board];
BoardCellState state = [board cellStateAtColumn:col andRow:row];
if (state == BoardCellStateStoneOne) {
// _stoneUnoView is a public property
// 'stoneOneCreation' creates a UIImageView of the stone
_stoneUnoView = [UIImageView stoneOneCreation];
[self addSubview:square];
[square addSubview:_stoneUnoView];
[_array insertObject:_stoneUnoView atIndex:0];
} else if (state == BoardCellStateStoneTwo) {
// same idea as above
_stoneDueView = [UIImageView stoneTwoCreation];
[self addSubview:square];
[square addSubview:_stoneDueView];
[_array insertObject:_stoneDueView atIndex:1];
} else {
// based on the 'init' method I write below, I assumed this
// would return an empty square cell
[self addSubview:square];
[_emptyArray insertObject:square atIndex:_i];
_i++;
}
}
}
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (UIView*)stoneUnoView {
return _stoneUnoView;
}
- (UIView*)stoneDueView {
return _stoneDueView;
}
- (id)initWithEmptyFrame:(CGRect)frame
column:(NSUInteger)column
row:(NSUInteger)row
board:(TwinstonesBoardModel*)board
{
self = [super initWithFrame:frame];
return self;
}
- (void)swipeLeft:(UIGestureRecognizer*)recognizer
{
NSLog(#"Swipe Left");
UIView* view = recognizer.view;
[self move:CGPointMake(-1, 0) withView:view];
}
- (void)move:(CGPoint)direction withView:view {
// whatever code I decide to put for stone movement
}
#end
I apologize for the (probably) unnecessary length, I've just trying to figure this out for a couple days and have had no luck. Here's the bullet points of what I'm trying to do:
setInititalStateMain sets the placements of two stones in a 5x5 grid
In GameViewController.m, I'm trying to capture the 'stoneUnoView' and 'stoneDueView' properties (set in the TwinstonesStoneView.m file), give them swipe gestures, and interact with them using the methods provided in TwinstonesStoneView.m.
Am I generating too many views? The catch is that everything works in terms of what I'm able to see on my IPhone when I run the program. The stones show up on my screen, but when I try to interact with them, not even the 'NSLog' message shows up in the console.
The 'stoneOneCreation' method (and ...two) are UIImageView's, but, as you can see, I store them in a UIView pointer.
I also used '[one setUserInteractionEnabled:YES]' (and ...two) but that didn't help either.
If I add the gesture recognizer to self.view, everything works (the displays of the stones, gameboard, and other graphics appears, and when I interact with ANY part of the screen, I output the directions to the console......just not stone-specific interaction).
Thank you so very much for putting up with all of this, this will really help if someone knows what's wrong. PS: all file #import's are correct, so that isn't a problem.
I am using XCode 7, Objective-C language, and developing for iOS
Anthony
You should add different instances of the swipe gesture recogniser to different instances of UIView.
UISwipeGestureRecognizer* swipeLeft1 = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(swipeLeft:)];
swipeLeft1.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft1.numberOfTouchesRequired = 1;
[one addGestureRecognizer:swipeLeft1];
UISwipeGestureRecognizer* swipeLeft2 = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(swipeLeft:)];
swipeLeft2.direction = UISwipeGestureRecognizerDirectionLeft;
swipeLeft2.numberOfTouchesRequired = 1;
[two addGestureRecognizer:swipeLeft2];
I assume it is because the gestire recogniser has readonly view property.
Hi may be user interaction is disabled for that views , gestures working only for the views which have the userInteractionEnable = YES.

Add image to subclassed UIImageView

I am trying to add a tap gesture from a subclassed UIImageView and then control the tap from the View Controller. I am not getting any compiling errors but "addSubview" is not displaying any image. How can make the UIImageView to be displayed?
If I try to control the tap and pan gestures from the subclassed UIImageVIew I have no problems but I would like to control these functions from the View Controller
Relevant code looks like this.
UIImageView subclass    
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
//self.userInteractionEnabled = YES;
previewController = [[PreviewController alloc]init];
[previewController self];
[self addSubview:character];
// Tap Initialization code
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:(PreviewController *)self.previewController action:#selector(addCharacter:)];
[self addGestureRecognizer:tap];
self.userInteractionEnabled = YES;
}
return self;
}
View Controller
- (void)addCharacter:(UITapGestureRecognizer *)t
{
NSLog(#"add character");
imageNSArray = [NSMutableArray array];
uiImg = [UIImage imageNamed:#"homer.png"];
CGPoint loc = [t locationInView:self.view];
character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[imageNSArray addObject:character];
//Locate the imageNSArray on frameImageView area only.
[self.view addSubview:character];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panCharacter:)];
[self.character addGestureRecognizer:pan];
character.userInteractionEnabled = YES;
}
put a view behind your subclassed imageView. Apply the gestureRecognition to the new View.
You can control your tap gesture from new view. Let me know if i am not clear, or if more info needed.
I think what you want is.. .you want to show a image when user taps on screen.
Do following steps:
1. drag & drop a tap gesture recognizer on default view of your view controller.
2. connect (ctrl +) gestureRecognizer with ViewController.m
Take a look at this code. (Just modified your code)
- (IBAction)onTap:(UITapGestureRecognizer *)sender {
NSLog(#"add character");
UIImage * uiImg = [UIImage imageNamed:#"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}
Let me know if this is not what you want…
Edit
better go for this code… No need to do above steps.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onTap:)];
[self.view addGestureRecognizer:tap];
self.view.userInteractionEnabled = YES;
}
- (void)onTap:(UITapGestureRecognizer *)sender
{
NSLog(#"add character");
UIImage * uiImg = [UIImage imageNamed:#"sel.png"];
CGPoint loc = [sender locationInView:self.view];
UIImageView * character = [[UIImageView alloc] initWithImage:uiImg];
character.center = loc;
[self.view addSubview:character];
}

Return position in NSArray from ScrollView

I'm trying to create a scrollview with an array of clickable UIImageView's. My goal is that when an ImageView is clicked, it returns which position in the array it occupies. The problem is that i don't know how to "catch" the position's number. How do I do that?
So far I have:
- (IBAction)respondToTapGesture:(UITapGestureRecognizer *)recognizer {
NSLog(#"%#",)//here is where i want to return the element's position.
}
-(void) preenchemenu {
[menu setContentSize:CGSizeMake(400, 91)];
int x=0;
imagensmenu=[NSArray arrayWithObjects:[UIImage imageNamed:#"teste2.tiff"],[UIImage imageNamed:#"teste2.tiff"], nil];
for (int i = 0; i <3; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,0 , 90, 91)];
x=x+90;
imageView.image = [imagensmenu objectAtIndex:i];
imageView.tag = 1000+ i;
imageView.userInteractionEnabled = YES;
imageView.multipleTouchEnabled = YES;
UITapGestureRecognizer *tapRecognizermenu = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(respondToTapGesture:)];
tapRecognizermenu.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapRecognizermenu];
[menu addSubview:imageView];
}
}
You can create an array to hold you imageViews and then find the position of your imageView in this array when it is tapped.
Add a property for this
#property (nonatomic, strong) NSMutableArray *imageViews;
initialise it in init
- (id)init...
{
self = [super init...
if (self) {
_imageViews = [NSMutableArray array];
}
return self;
}
Then amend your current method slightly to also add the imageViews to this array as well as a subview of the menu
[self.imageViews addObject:imageView];
[menu addSubview:imageView];
Then in your gesture recognizer call back you can do
- (void)respondToTapGesture:(id)sender;
{
UIView *view = [sender view];
NSLog(#"%d", [self.imageViews indexOfObject:view]);
}
Just find the index by
- (IBAction)respondToTapGesture:(UITapGestureRecognizer *)recognizer
{
UIView *view = recognizer.view;
NSLog(#"Index of image in array is %d", view.tag-1000);
}

Switch image to full screen on ipad

I have a lot of trouble to accomplish this task on an ipad : when double tape on an image switch this image to full screen and when double taping again come back to the original display, same thing using pinching. I'm using UIGestureRecognizer to try to do this. Thanks for your help.
GesturesViewController.h
#import <UIKit/UIKit.h>
#interface GesturesViewController : UIViewController
<UIActionSheetDelegate>{
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) UIImageView *imageView;
#end
GesturesViewController.m
#import "GesturesViewController.h"
#import "GesturesAppDelegate.h"
#implementation GesturesViewController
#synthesize imageView;
CGRect originalFrame,fullScreenFrame;
BOOL isFullScreenMode;
- (void)viewDidLoad {
// Loading test image
imageView.image = [UIImage imageNamed:#"image1.jpg"];
//---tap gesture---
isFullScreenMode = NO;
originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);
//changes
fullScreenFrame = CGRectMake(0,0,768,1004);
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
//---pinch gesture---
UIPinchGestureRecognizer *pinchGesture =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)];
[imageView addGestureRecognizer:pinchGesture];
[pinchGesture release];
[super viewDidLoad];
}
//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
// HOW TO ACCOMPLISH THIS PART
if (isFullScreenMode)
[imageView setFrame:originalFrame];
else
[imageView setFrame:fullScreenFrame];
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
isFullScreenMode = !isFullScreenMode;
NSLog(#"Image View : %#",imageView);
}
//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender {
CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
if (sender.state == UIGestureRecognizerStateEnded){
// HOW TO ACCOMPLISH THIS ---
if (factor > 1 && !isFullScreenMode) {
//---pinching in---
[imageView setFrame:fullScreenFrame];
} else {
//---pinching out---
[imageView setFrame:originalFrame];
}
isFullScreenMode = !isFullScreenMode;
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
}
NSLog(#"Image View : %#",imageView);
}
- (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 {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[images release];
[imageView release];
[super dealloc];
}
#end
Thanks.
- (void)toggleZoom:(UITapGestureRecognizer *)gestureRecognizer
{
if (proxyView)
{
CGRect frame =
[proxyView.superview
convertRect:self.view.frame
fromView:self.view.window];
self.view.frame = frame;
CGRect proxyViewFrame = proxyView.frame;
[proxyView.superview addSubview:self.view];
[proxyView removeFromSuperview];
[proxyView autorelease];
proxyView = nil;
self.view.frame = proxyViewFrame;
}
else
{
proxyView = [[UIView alloc] initWithFrame:self.view.frame];
proxyView.hidden = YES;
proxyView.autoresizingMask = self.view.autoresizingMask;
[self.view.superview addSubview:proxyView];
CGRect frame =
[self.view.window
convertRect:self.view.frame
fromView:proxyView.superview];
[self.view.window addSubview:self.view];
self.view.frame = frame;
self.view.frame = self.view.window.bounds;
}
}
I have selected only necessary portion of the code...... its from ZoomingViewController....
If you see its same as we have discussed earlier..... but with few improvements.......
For doing this you have to first store your original frame size somewhere globally so that you can reassing it later on.
you need create two global frames
CGRect originalFrame, fullScreenFrame;
//in viewDidLoad initialize these frames... originalFrame with imageView frame and
fullScreenFrame with the iPad window coordinates........ but remeber this can distort the
aspect ratio so just calculate the aspect ratio of original image by using its height and
width and accordingly create the full screen frame for the image.......
and just assign these frames in your gesture action.
Thanks,
in
viewDidLoad
originalFrame = imageView.frame;
or
originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);
appDelegate <----- get the instance of your appdelegate object so that we can retrieve the window object.......
UIWindow *tempWindow = [appDelegate window];
fullScreenFrame = CGRectMake(tempWindow .frame.origin.x,tempWindow .frame.origin.y,tempWindow .frame.size.width,tempWindow.frame.size.height);
//**in event just set frame of the imageView-- for knowing the current state-- whether its fullScreen or original frame we need to have a flag........it should be global...
so declare a global flag ....BOOL isFullScreenMode and initialize it as NO in
viewDidLoad
isFullScreenMode = NO;
in gesture actions just check this flag and write following...
if (isFullScreenMode)
[imageView setFrame:originalFrame];
else
[imageView setFrame:fullScreenFrame];
isFullScreenMode = !isFullScreenMode;
#implementation ImageFullScreen
#synthesize myImage;
#import "GesturesViewController.h"
#import "GesturesAppDelegate.h"
#implementation GesturesViewController
#synthesize imageView;
CGRect originalFrame,fullScreenFrame;
BOOL isFullScreenMode;
- (void)viewDidLoad {
// Loading test image
imageView.image = [UIImage imageNamed:#"image1.jpg"];
//---tap gesture---
isFullScreenMode = NO;
originalFrame = CGRectMake(imageView.frame.origin.x,imageView.frame.origin.y,imageView.frame.size.width,imageView.frame.size.height);
//changes
fullScreenFrame = CGRectMake(0,0,768,1004);
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
//---pinch gesture---
UIPinchGestureRecognizer *pinchGesture =
[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)];
[imageView addGestureRecognizer:pinchGesture];
[pinchGesture release];
[super viewDidLoad];
}
//---handle tap gesture---
-(IBAction) handleTapGesture:(UIGestureRecognizer *) sender {
// HOW TO ACCOMPLISH THIS PART
if (isFullScreenMode)
[imageView setFrame:originalFrame];
else
[imageView setFrame:fullScreenFrame];
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
isFullScreenMode = !isFullScreenMode;
NSLog(#"Image View : %#",imageView);
}
//---handle pinch gesture---
-(IBAction) handlePinchGesture:(UIGestureRecognizer *) sender {
CGFloat factor = [(UIPinchGestureRecognizer *) sender scale];
if (sender.state == UIGestureRecognizerStateEnded){
// HOW TO ACCOMPLISH THIS ---
if (factor > 1 && !isFullScreenMode) {
//---pinching in---
[imageView setFrame:fullScreenFrame];
} else {
//---pinching out---
[imageView setFrame:originalFrame];
}
isFullScreenMode = !isFullScreenMode;
[imageView setCenter:CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2)];
}
NSLog(#"Image View : %#",imageView);
}

Resources