Decrease size of UIButtons on Screen upon Pinch Gesture - ios

I have couple of UIButtons on the screen. If a pinch gesture occurs, I want to decrease the size of the buttons (Like a zoom-out effect) and add more buttons. How would I implement it?

I am typing this directly on StackOverflow, so there may be typos.
These features are left as an exercise for the OP:
Cumulatively scaling down with successive pinches. You must have another private property to hold the current scale value.
Adding new buttons after the zooming out effect.
Code:
#interface MyViewController : UIViewController
#property(nonatomic, strong) NSMutableArray* buttons;
- (void)pinched:(UIPinchGestureRecognizer*)gesture;
#end
#implementation MyViewController
- (void)loadView {
[super loadView];
self.buttons = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(
0.0f,
(44.0f + 10.0f) * (CGFloat)i,
100.0f,
44.0f
)];
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[self.buttons addObject:button];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinched:)];
[self.view addGestureRecognizer:pinch];
}
- (void)pinched:(UIPinchGestureRecognizer*)gesture {
if (gesture.scale > 1.0) {
return;
}
for (UIButton* button in self.buttons) {
[UIView
animateWithDuration:1.0
animations:^void() {
button.transform = CGAffineTransformMakeScale(0.5, 0.5);
}
];
}
}
#end

Related

Scrollview not loading additional Images

I have a UIView that I am using as a simple onboarding view. I simply shows n images, that the user can swipe through.
The only image that loads is the very first image "OnBoard-1". The other images are there when I debug the what is being added to the image view.
What am I doing wrong?
.h
#import <UIKit/UIKit.h>
#interface OnBoardingView : UIView
- (void)setImages:(NSArray *)newImages;
#end
Here is the .m file
#import "OnBoardingView.h"
#interface OnBoardingView () <UIScrollViewDelegate>
{
UIPageControl *pageControl;
NSArray *contentImages;
}
#property (nonatomic, retain) UIPageControl *pageControl;
#property (nonatomic, retain) NSArray *contentImages;
#end
#implementation OnBoardingView
#synthesize pageControl;
#synthesize contentImages;
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) { }
return self;
}
#pragma mark - Override contentImages setter
- (void)setImages:(NSArray *)newImages {
if (newImages != self.contentImages) {
self.contentImages = newImages;
[self setup];
}
}
#pragma mark - Carousel setup
- (void)setup {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
[scrollView setDelegate:self];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setPagingEnabled:YES];
[scrollView setBounces:NO];
CGSize scrollViewSize = scrollView.frame.size;
for (NSInteger i = 0; i < [self.contentImages count]; i++) {
CGRect slideRect = CGRectMake(scrollViewSize.width * i, 0, scrollViewSize.width, scrollViewSize.height);
UIView *slide = [[UIView alloc] initWithFrame:slideRect];
[slide setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
[imageView setImage:[UIImage imageNamed:[self.contentImages objectAtIndex:i]]];
NSLog(#"Image named: %#", [self.contentImages objectAtIndex:i]);
[slide addSubview:imageView];
[scrollView addSubview:slide];
}
UIPageControl *tempPageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollViewSize.height - 20, scrollViewSize.width, 20)];
[self setPageControl:tempPageControll];
[self.pageControl setNumberOfPages:[self.contentImages count]];
[scrollView setContentSize:CGSizeMake(scrollViewSize.width * [self.contentImages count], scrollViewSize.height)];
[self addSubview:scrollView];
[self addSubview:self.pageControl];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
[self.pageControl setCurrentPage:page];
}
#end
You initialise the imageView with the frame of the scrollView, that's in any case not right and may be the cause of your problem.
BTW:
Your property handling looks a bit strange (why the synthesizing instead of just using a normal property only?), and why do you compare the arrays by pointer (newImages != self.contentImages)?
If you want to show the images in paging directly you can assign the number of pages count.And apply the swipe gesture(left and right) to imageview.And based on left and right swipe,you can change the image of imageview.

UIScrollview does not react to any touch?

I am trying to do a simple scroll, but the views do not move after a touch, I am not sure why, the scrollview should handle the gesture, but something might be missing. Would someone know where?
Here is the code : I create a small horizontal scroll view, with some views inside. The views appear well, I am testing it on a device for the touch :
- (void)viewDidLoad {
[super viewDidLoad];
//horizontal scroll view
HorizScroll *ho = [[HorizScroll alloc] initWithFrame:CGRectMake(0, 0, 500, 100)];
for ( int i=0; i<3; i++){
MyView* mv = [[MyView alloc] init];
[ho addSubview:mv];
}
//ho.zoomScale = 0.3f;
[self.view addSubview:ho];
}
#implementation HorizScroll
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
-(void)addSubview:(UIView *)view{
[super addSubview:view];
NSUInteger numSubviews = self.subviews.count;
[view setFrame:CGRectMake(CGRectGetWidth(view.bounds)*(numSubviews),
0,
CGRectGetWidth(view.bounds),
CGRectGetHeight(view.bounds) )];
[self setContentSize:CGSizeMake(CGRectGetWidth(view.bounds)*(numSubviews),
CGRectGetHeight(view.bounds) )];
}
#implementation MyView
-(int)getRandomNumberBetween:(int)from to:(int)pto {
return (int)(from + arc4random() % (pto-from+1));
}
-(instancetype)init{
if ( self = [super init] ){
CGFloat red = [self getRandomNumberBetween:1 to:255];
self.backgroundColor = [UIColor colorWithRed:red/256.0
green:[self getRandomNumberBetween:1 to:255]/256.0
blue:[self getRandomNumberBetween:1 to:255]/256.0
alpha:1.0];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
if ( self = [super initWithFrame:frame] ){
self.frame = CGRectMake(counterX, 0, 100, 100);
counterX += 50;
}
return self;
}
You need to set contentSize of scrollview to be larger than its frame size. so add this line after [self.view addSubview:ho].
ho.contentSize = CGSizeMake(501.f, 100.f);
or before [self.view addSubview:ho] and comment out the line:
[self setContentSize:CGSizeMake(CGRectGetWidth(view.bounds)*(numSubviews),
CGRectGetHeight(view.bounds))];
which is not necessary since you can set it after all subviews are added.

UITapGestureRecognizer not responding on animated subview

I have a simple program that creates a subview and animates it across the screen.
As part of this program, I would like to add functionality when the subview is tapped. I am using the following method to create the subview, add the UITapGestureRecognizer and then animate the subview:
int randomName = arc4random() % ([pieceNames count] - 1);
int animationDuration = arc4random() % 5 + 5 ;
NSString *randomPiece = [pieceNames objectAtIndex:randomName];
float yStart = arc4random() % 650;
float yEnd = arc4random() % 650;
UIView *piece = [[PieceView alloc]initWithFrame:CGRectMake(100.0, yStart, 75.0, 75.0)];
[piece setValue:randomPiece forKey:#"name"];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self
action:#selector(handleTouch:)];
[piece addGestureRecognizer:recognizer];
[[self view] addSubview:piece];
[UIView animateWithDuration:animationDuration
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^(void){
piece.center = CGPointMake(950.0, yEnd);
} completion:^(BOOL done){
[piece removeFromSuperview];
}];
Here is the code that handles the tap:
PieceView *pv = (PieceView *) recognizer.view;
NSLog(#"%# was tapped", pv.name);
What happens is when a PieceView is touched the program does not respond. However, if I remove the animation block then the program responds to the tap.
Why does the UITapGestureRecognizer fail to respond to PieceView when it is animated?
I struggled with this same problem, and it boils down to this: the animated view only ever is in two places: the starting position, and the ending position. Core Animation simply renders the view's layer in interpolated positions between the start and end points over a period of time.
It's almost like when you look to the stars and realize that what you see is not actually what is happening right now. :)
Luckily, the solution is pretty simple. You can put a tap recognizer on the superview and then inspect your animated view's presentationLayer (which does give you an accurate frame at any point in time) to determine if your tap is a hit or not.
I've built a simple UIViewController that demonstrates both the problem and solution:
#import <UIKit/UIKit.h>
#interface MSMViewController : UIViewController
#end
And the implementation:
#import "MSMViewController.h"
#interface MSMViewController ()
#property (nonatomic, strong) UIView *animatedView;
#end
#implementation MSMViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect startFrame = CGRectMake(125, 0, 70, 70);
CGRect endFrame = CGRectMake(125, 400, 70, 70);
// draw a box to show where the animated view begins
UIView *startOutlineView = [[UIView alloc] initWithFrame:startFrame];
startOutlineView.layer.borderColor = [UIColor blueColor].CGColor;
startOutlineView.layer.borderWidth = 1;
[self.view addSubview:startOutlineView];
// draw a box to show where the animated view ends
UIView *endOutlineView = [[UIView alloc] initWithFrame:endFrame];
endOutlineView.layer.borderColor = [UIColor blueColor].CGColor;
endOutlineView.layer.borderWidth = 1;
[self.view addSubview:endOutlineView];
self.animatedView = [[UIView alloc] initWithFrame:startFrame];
self.animatedView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.animatedView];
[UIView animateWithDuration:10 delay:2 options:UIViewAnimationOptionAllowUserInteraction animations:^{
self.animatedView.frame = endFrame;
} completion:nil];
// this gesture recognizer will only work in the space where endOutlintView is
UITapGestureRecognizer *boxTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(boxTap:)];
[self.animatedView addGestureRecognizer:boxTap];
// this one will work
UITapGestureRecognizer *superviewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(superviewTap:)];
[self.view addGestureRecognizer:superviewTap];
}
- (void)boxTap:(UITapGestureRecognizer *)tap {
NSLog(#"tap. view is at %#", NSStringFromCGPoint(self.animatedView.frame.origin));
}
- (void)superviewTap:(UITapGestureRecognizer *)tap {
CGRect boxFrame = [self.animatedView.layer.presentationLayer frame];
if (CGRectContainsPoint(boxFrame, [tap locationInView:self.view])) {
NSLog(#"we tapped the box!");
}
}
#end
The solution is much simpler, you just need to set the animation's option UIViewAnimationOptions.allowUserInteraction.
UIView.animate(withDuration: duration, delay: 0.1, options: [.allowUserInteraction], animations: {
...
}, completion: { (completed) in
...
}

Increment UISlider by 1 in range 1 to 100

I am new to iPhone,
How do I have my UISlider go from 1 to 100 in increments of 1?
slider = [[UISlider alloc] init];
[slider addTarget:self action:#selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 1;
slider.maximumValue = 100;
slider.continuous = YES;
slider.value = 0.0;
- (IBAction)sliderChange:(id)sender{
NSLog(#"slider.value=%f",slider.value);
}
When i slide my log shows...
slider.value = 1.000000
slider.value = 1.123440
slider.value = 1.234550
slider.value = 1.345670
slider.value = 1.567890
.
.
.
I want slider value as 1.0 , 2.0 , 3.0 and so on...
//Only generate update events on release
slider.continuous = NO;
//Round the value and set the slider
- (IBAction)sliderChange:(id)sender
{
int rounded = sender.value; //Casting to an int will truncate, round down
[sender setValue:rounded animated:NO];
NSLog(#"%f", sender.value);
}
you could try this:
float RoundValue(UISlider * slider) {
return roundf(slider.value * 2.0) * 1;
}
yourSlider.minimumValue = 1;
yourSlider.maximumValue = 100;
[yourSlider addTarget:self action:#selector(roundValue) forControlEvents:UIControlEventValueChanged];
- (void)roundValue{
yourSlider.value = round(yourSlider.value);
}
also see this bellow answer helpful toy you,give some idea...
UISlider increments
and also another which may give you some idea..
UISlider with increments of 5
I know i'm late to the party but I've also had to do this recently. Here is how I've done it in swift using XCode 8.3.2:
#IBAction func sliderValueChanged(_ slider: UISlider) {
label.text = Int(slider.value)
}
Simples! I know I could have used 'Any' instead of 'UISlider', but I think this makes my intention clearer ;)
change
` NSLog(#"slider.value=%f",slider.value);`
to
NSLog(#"slider.value=%.f",slider.value);
if you want one digit then :
NSLog(#"slider.value=%.0f",slider.value);
but this will give you results like 1.0,1.1,....
Here is simple Example of UISlider
headereFile.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIScrollViewDelegate,UITextFieldDelegate>
{
IBOutlet UISlider *slider;
IBOutlet UILabel *lbl;
IBOutlet UITextField *txtField;
IBOutlet UIButton *btn;
IBOutlet UISwitch *swich;
IBOutlet UIScrollView *scView;
}
#property(nonatomic,retain)UISlider *slider;
#property(nonatomic,retain)UILabel *lbl;
#property(nonatomic,retain)UITextField *txtField;
#property(nonatomic,retain)UIButton *btn;
#property(nonatomic,retain)UIScrollView *scView;
#property(nonatomic,retain)UISwitch *swich;
-(IBAction)sliderChanged:(UISlider *)slider;
-(IBAction)ButtonPressed:(id)sender;
-(IBAction)showTextField:(id)sender;
#end
implementFile.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize slider,lbl,txtField,btn,scView,swich;
- (void)viewDidLoad
{
self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
self.scView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,320,460)];
self.scView.delegate=self;
self.scView.contentSize=CGSizeMake(320,465);
[self.view addSubview:self.scView];
self.slider=[[UISlider alloc] initWithFrame:CGRectMake(50,270,220,30)];
self.slider.minimumValue=0.00;
self.slider.maximumValue=100.00;
self.slider.thumbTintColor=[UIColor redColor];
[self.slider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
[self.scView addSubview:self.slider];
self.lbl=[[UILabel alloc] initWithFrame:CGRectMake(135, 290,200,30)];
self.lbl.backgroundColor=[UIColor clearColor];
NSString *str=[NSString stringWithFormat:#"%.f",[self.slider value]];
self.lbl.text=str;
[self.scView addSubview:self.lbl];
self.txtField=[[UITextField alloc] initWithFrame:CGRectMake(50, 220,150,30)];
self.txtField.borderStyle=UITextBorderStyleRoundedRect;
self.txtField.delegate=self;
self.txtField.returnKeyType=UIReturnKeyDone;
self.txtField.placeholder=#"Enter 1 to 100";
[self.scView addSubview:self.txtField];
self.btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btn.frame=CGRectMake(205, 215,64, 37);
[self.btn setTitle:#"Change" forState:UIControlStateNormal];
[self.btn addTarget:self action:#selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.scView addSubview:self.btn];
self.swich=[[UISwitch alloc] initWithFrame:CGRectMake(124,140, 75,40)];
[self.swich addTarget:self action:#selector(showTextField:) forControlEvents:UIControlEventValueChanged];
self.swich.on=YES;
/*
((UILabel *)[[[[[[self.swich subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:0]).text = #"Foo";
((UILabel *)[[[[[[self.swich subviews] lastObject] subviews] objectAtIndex:2] subviews] objectAtIndex:1]).text = #"Bar";
*/
[self.scView addSubview:self.swich];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)showTextField:(id)sender
{
if (self.swich.on)
{
[self.swich setOn:YES animated:YES];
self.txtField.hidden=NO;
self.btn.hidden=NO;
}
else
{
[self.swich setOn:NO animated:YES];
self.txtField.hidden=YES;
self.btn.hidden=YES;
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
self.scView.frame=CGRectMake(0, 0,320,230);
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[self.txtField resignFirstResponder];
self.scView.frame=CGRectMake(0, 0,320,460);
return YES;
}
-(IBAction)sliderChanged:(UISlider *)slider;
{
NSString *str=[NSString stringWithFormat:#"%.f",[self.slider value]];
self.lbl.text=str;
}
-(IBAction)ButtonPressed:(id)sender
{
NSString *values=[self.txtField text];
float Values=[values floatValue];
if (Values<=100)
{
self.slider.value=Values;
self.lbl.text=values;
}
else
{
UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:#"NSURLConnection " message:#"Plz...Entre Proper Value !!!" delegate: self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[connectFailMessage show];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I use this bit of code when I want the increment to be 0.5. It works with 1 or any other number
- (void) getSliderValue:(UISlider *)paramSender{
float increment = 0.5;
if ([paramSender isEqual:self.slider]){ //remove if you only have one slider
float newValue = paramSender.value /increment;
paramSender.value = floor(newValue) * increment;
}
NSLog(#"Current value of slider is %f", paramSender.value);
}

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