Switch image to full screen on ipad - 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);
}

Related

Show Image Bigger on tap gesture in IOS

I have used third party lib to show array of images in a slider . The images in an array comes from server, now i want to show the image in a bigger image view on a tap gesture. When anyone of the image user tap it should be shown in big imageview. I have tried some code but it is not working. My code is,
int i=0;
tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(expandImage:)];
tap.numberOfTapsRequired = 1;
[_ImageView setUserInteractionEnabled:YES];
[_ImageView addGestureRecognizer:tap];
tap = [_imagesData objectAtIndex:i];
NSLog(#"TTT %#",tap);
[self.view addSubview:_fullImage];
The method is ,
-(void)expandImage:(UITapGestureRecognizer*)recogniser
{
[self.view addSubview:_fullImage];
UIButton *closeButton = [[UIButton alloc]init];
[closeButton setTitle:#"Close" forState:UIControlStateNormal];
// [closeButton addTarget:self action:#selector(Closetab) forControlEvents:UIControlEventTouchUpInside];
[_fullImage addSubview:closeButton];
UIImageView *photoView = [[UIImageView alloc]init];
[photoView setBackgroundColor:[UIColor blueColor]];
[_fullImage addSubview:photoView];
// photoView.accessibilityIdentifier = #"nature2.png";
[_fullImage addSubview:_fullImage];
}
here I attached the sample project , customize yourself
step1
create the one full screen view for preview the image (use one UIView, one close btn, one imageview)
#interface ViewController () <UIScrollViewDelegate, TAPageControlDelegate>
{
NSTimer *timer;
NSInteger index;
//
IBOutlet UIImageView *imgfull;
IBOutlet UIView *fullimagevie;
IBOutlet UIButton *btnClose;
}
step2
add the gesture for tap the image event and set the frame of your duplicate view as bottom of viewcontroller
- (void)viewDidLoad
{
[super viewDidLoad];
[self setviewframe:self.view.frame.size.height + 10];
self.imagesData = #[#"image1.jpg", #"image2.jpg", #"image3.jpg"];
for(int i=0; i<self.imagesData.count;i++){
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame))];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(expandImage:)];
tap.numberOfTapsRequired = 1;
tap.view.tag = i;
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:tap];
[self.scrollView addSubview:imageView];
}
step3
handle the image tap for preview the image
-(void)expandImage:(UITapGestureRecognizer*)recogniser
{
imgfull.image = [UIImage imageNamed:[self.imagesData objectAtIndex:recogniser.view.tag]];
[UIView transitionWithView:fullimagevie
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self setviewframe:0];
}
completion:NULL];
}
step4
dismiss the bigger image view on tap the close button
- (IBAction)btnClose:(UIButton *)sender {
[UIView transitionWithView:fullimagevie
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[self setviewframe:self.view.frame.size.height + 10];
}
completion:NULL];
}
step5
create the common method for set the frame for your bigger imageview
-(void)setviewframe:(CGFloat)coordinate{
CGRect modifyframe = fullimagevie.frame;
modifyframe.origin.y = coordinate;
fullimagevie.frame = modifyframe;
}

How to create custom slide menu without third party library.?

I want to implement slide menu in my iOS app like drawer (Andriod). I went through a tutorial, but all of them are using third party libraries. Is there any possibility to create a custom slide menu. I tried to create it with the following code, but it's only working with xib file:
- (IBAction)sidemenu:(id)sender
{
[UIView animateWithDuration:0.50f animations:^{
view.frame = self.view.frame;
} completion:^(BOOL finished) {
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipGestureLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
}];
}
- (void)SwipGestureAction
{
UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipGestureRightAction:)];
swiperight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
}
#pragma mark AddSwipeGestureLeftAndRight
- (void)SwipGestureRightAction:(UISwipeGestureRecognizer *)swipeRight
{
[UIView animateWithDuration:0.50f animations:^{
view.frame = self.view.frame;
} completion:^(BOOL finished) {
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipGestureLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
}];
}
- (void)SwipGestureLeftAction:(UISwipeGestureRecognizer *)swipeRight
{
[UIView animateWithDuration:0.50f animations:^{
[view setFrame:CGRectMake(self.view.frame.origin.x - self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
} completion:^(BOOL finished){
[self.view removeGestureRecognizer:swipeLeft];
}];
}
Here is what I have for you:
I made a super class for all my slide menus in all projects. It manages the showing and hiding of the slide menu, and handles orientation changes. It slides in from left on top of the current view, and it partially obscures the remainder of the view with a dark transparent background.
If you ever need other behaviour (like pushing out the current view) just override the animation part.
My slide menu is a Singleton because in our applications we only use one slide menu on every screen.
#import <UIKit/UIKit.h>
#interface IS_SlideMenu_View : UIView <UIGestureRecognizerDelegate>
{
UIView* transparentBgView;
BOOL hidden;
int lastOrientation;
}
#property (strong, nonatomic) UIView *menuContainerV;
+ (id)sharedInstance;
- (BOOL)isShown;
- (void)hideSlideMenu;
- (void)showSlideMenu;
#end
#import "IS_SlideMenu_View.h"
#implementation IS_SlideMenu_View
+ (id)sharedInstance
{
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[[self class] alloc] init];
});
return _sharedInstance;
}
- (instancetype)initWithFrame:(CGRect)frame
{
frame = [[[UIApplication sharedApplication] delegate] window].frame;
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
transparentBgView = [[UIView alloc] initWithFrame:frame];
[transparentBgView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];
[transparentBgView setAlpha:0];
transparentBgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognized:)];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognized:)];
[transparentBgView addGestureRecognizer:tap];
[transparentBgView addGestureRecognizer:pan];
[self addSubview:transparentBgView];
frame.size.width = 280;
self.menuContainerV = [[UIView alloc] initWithFrame:frame];
CALayer *l = self.menuContainerV.layer;
l.shadowColor = [UIColor blackColor].CGColor;
l.shadowOffset = CGSizeMake(10, 0);
l.shadowOpacity = 1;
l.masksToBounds = NO;
l.shadowRadius = 10;
self.menuContainerV.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self addSubview: self.menuContainerV];
hidden = YES;
}
//----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
lastOrientation = [[UIDevice currentDevice] orientation];
return self;
}
//********** ORIENTATION CHANGED **********
- (void)orientationChanged:(NSNotification *)note
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
NSLog(#"%ld",orientation);
if(!hidden && lastOrientation != orientation){
[self hideSlideMenu];
hidden = YES;
lastOrientation = orientation;
}
}
}
- (void)showSlideMenu {
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
self.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
[self.menuContainerV setTransform:CGAffineTransformMakeTranslation(-window.frame.size.width, 0)];
[window addSubview:self];
// [[UIApplication sharedApplication] setStatusBarHidden:YES];
[UIView animateWithDuration:0.5 animations:^{
[self.menuContainerV setTransform:CGAffineTransformIdentity];
[transparentBgView setAlpha:1];
} completion:^(BOOL finished) {
NSLog(#"Show complete!");
hidden = NO;
}];
}
- (void)gestureRecognized:(UIGestureRecognizer *)recognizer
{
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
[self hideSlideMenu];
} else if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
static CGFloat startX;
if (recognizer.state == UIGestureRecognizerStateBegan) {
startX = [recognizer locationInView:self.window].x;
} else
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGFloat touchLocX = [recognizer locationInView:self.window].x;
if (touchLocX < startX) {
[self.menuContainerV setTransform:CGAffineTransformMakeTranslation(touchLocX - startX, 0)];
}
} else
if (recognizer.state == UIGestureRecognizerStateEnded) {
[self hideSlideMenu];
}
}
}
- (void)hideSlideMenu
{
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
window.backgroundColor = [UIColor clearColor];
[UIView animateWithDuration:0.5 animations:^{
[self.menuContainerV setTransform:CGAffineTransformMakeTranslation(-self.window.frame.size.width, 0)];
[transparentBgView setAlpha:0];
} completion:^(BOOL finished) {
[self removeFromSuperview];
[self.menuContainerV setTransform:CGAffineTransformIdentity];
// [[UIApplication sharedApplication] setStatusBarHidden:NO];
hidden = YES;
NSLog(#"Hide complete!");
}];
}
- (BOOL)isShown
{
return !hidden;
}
#end
Subclasses only need to add subviews to the menuContainerV view, and manage them.
An example:
I created a subclass that has an header view and a table view as its content. I created the content view in a xib, and the owner of the xib is this subclass. This way I can bind outlets to the xib.
#import "IS_SlideMenu_View.h"
#interface CC_SlideMenu_View : IS_SlideMenu_View<UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UIView *headerView;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
...
#end
When the slide menu gets instantiated I load the xib and add the content view to the menuContainerV view.
#import "CC_SlideMenu_View.h"
#implementation CC_SlideMenu_View
- (instancetype)init
{
self = [super init];
if (self) {
UIView *v = [[[NSBundle mainBundle] loadNibNamed:#"CC_SlideMenu_View" owner:self options:nil] firstObject];
v.frame = self.menuContainerV.bounds;
[self.menuContainerV addSubview:v];
self.tableView.backgroundColor = [UIColor darkGrayColor];
}
return self;
}
...
#end
The result is something like this.

Decrease size of UIButtons on Screen upon Pinch Gesture

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

Add action to a programmatically created ImageView

I have created image view through code like this -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:dot];
}
I want to add User Interaction to this UIImageView and then create a selector or action for this UIImageView when tapped how is this done?
It depends on what kind of action you want achieve. Generally speaking you'd be using UIGestureRecognizer. For instance if you want your image to respond to a tap gesture then you'd have something like the following.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
dotArray = [NSMutableArray alloc]init];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
for(int i = 0; i< 30; i++) {
UIImageView *dot =[[UIImageView alloc] ...
dot.image=[UIImage imageNamed:#"draw.png"];
dot.tag = i; //identify dot image.
[self.view addSubview:dot];
[dotArray addObject:dot];
[dot addGestureRecognizer:tapGesture];
...
}
[tapGesture release];
}
Then the methods to handle tap gesture...
-(void)handleTapGesture:(id)sender {
UITapGestureRecognizer * tapGesture = (UITapGestureRecognizer*)sender;
for(int i = 0; i<[dotArray count]; i++) {
UIImageView * dot = (UIImageView*)[dotArray objectAtIndex:i];
if(dot.tag == [tapGesture view].tag) {
//fade out animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
dot.alpha = 0.0f;
[UIView commitAnimations];
}
For this to work, you need to make an array of dots and declare it as instance variable, otherwise the method can't access the dot.
Try like this:-
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:dot];
1) To implement the number of touch required is 1 refer this:-
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[dot addGestureRecognizer:doubleTapRecognizer];
- (void)doubleTapped:(UITapGestureRecognizer*)recognizer
{
}
2) To implement the number of touch required is 2 refer this:-
UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(scrollViewTwoFingerTapped:)];
twoFingerTapRecognizer.numberOfTapsRequired = 1;
twoFingerTapRecognizer.numberOfTouchesRequired = 2;
[dot addGestureRecognizer:twoFingerTapRecognizer];
- (void)twoFingerTapped:(UITapGestureRecognizer*)recognizer
{
}
Thank you all of you that helped me I put everyones answers together and got this as my final code.
//
// ViewController.m
// InvaderRush
//
// Created by Ajay Venkat on 13/12/2014.
// Copyright (c) 2014 AJTech. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
{
NSArray *dotArray;
}
#end
#implementation ViewController
- (void)viewDidLoad {
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,100,100)];
dot.image=[UIImage imageNamed:#"invader.jpg"];
[self.view addSubview:dot];
dot.tag = 1; //identify dot image.
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapped:)];
doubleTapRecognizer.numberOfTouchesRequired = 1;
[dot addGestureRecognizer:doubleTapRecognizer];
dot.userInteractionEnabled = YES;
NSMutableArray *images =[[NSMutableArray alloc] initWithObjects: dot,nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)doubleTapped:(id)sender {
UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
UIImageView *imageView = (UIImageView *)recognizer.view;
if(imageView.tag==1) {
[imageView setImage:[UIImage imageNamed:#"space_invader.jpg"]];
}
}
-(void)handleTapGesture:(id)sender {
}
#end

UIScroll center image

I've edited this question a few times, but can still not get my images to center inside a uiview. I want them to be able to rotate like the photos app and display the correct size when a user brings them up. Here is what I'm working with:
In my PhotoViewController.h
#import <UIKit/UIKit.h>
#protocol PhotoViewControllerDelegate <NSObject>
- (void)toggleChromeDisplay;
#end
#interface PhotoViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>
#property (nonatomic, strong) UIImage *photo;
#property (nonatomic) NSUInteger num;
//Delegate
#property (nonatomic, strong) id<PhotoViewControllerDelegate> photoViewControllerDelegate;
#property (nonatomic, strong) UIImageView *photoImgView;
#property (nonatomic, strong) UIScrollView *scrollView;
#end
In my PhotoViewController.m:
#import "PhotoViewController.h"
#interface PhotoViewController ()
#end
#implementation PhotoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//todo
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect screenBounds = self.view.bounds;
//scroll view
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenBounds.size.width, screenBounds.size.height)];
_scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
_scrollView.pagingEnabled = NO;
_scrollView.scrollEnabled = YES;
[_scrollView setBackgroundColor:[UIColor blueColor]];
//Zoom Properties
_scrollView.maximumZoomScale = 6.0;
_scrollView.minimumZoomScale = 1.0;
_scrollView.bouncesZoom = YES;
_scrollView.delegate = self;
_scrollView.zoomScale = 1.0;
_scrollView.contentSize = _photoImgView.bounds.size;
[_scrollView setShowsHorizontalScrollIndicator:NO];
[_scrollView setShowsVerticalScrollIndicator:NO];
[self photoBounds];
[self.view addSubview: _scrollView];
//Add the UIImageView
_photoImgView = [[UIImageView alloc] initWithImage:_photo];
_photoImgView.image = _photo;
_photoImgView.clipsToBounds = YES;
_photoImgView.contentMode = UIViewContentModeScaleAspectFit;
_photoImgView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin);
[_photoImgView setUserInteractionEnabled:YES];
[_scrollView addSubview: _photoImgView];
//Set up Gesture Recognizer
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
UITapGestureRecognizer *dTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dTapGestureCaptured:)];
dTap.numberOfTapsRequired = 2;
[singleTap requireGestureRecognizerToFail:dTap];
//Gesture Methods
[self.scrollView addGestureRecognizer:singleTap];
[self.scrollView addGestureRecognizer : dTap];
}
- (void)photoBounds
{
UIInterfaceOrientation statusbar = [[UIApplication sharedApplication] statusBarOrientation];
CGSize photoBounds = _photo.size;
CGSize scrollBounds = self.view.bounds.size;
CGRect frameToCenter = [_photoImgView frame];
float newHeight = (scrollBounds.width / photoBounds.width) * photoBounds.height;
float newWidth = (scrollBounds.height / photoBounds.height) * photoBounds.width;
float yDist = fabsf(scrollBounds.height - newHeight) / 2;
float xDist = fabsf(scrollBounds.width - newWidth) / 2;
//Width Larger
if (photoBounds.width >=photoBounds.height) {
NSLog(#"portrait width");
_photoImgView.frame = CGRectMake(0, 0, scrollBounds.width, newHeight);
frameToCenter.origin.y = yDist;
}
//Height Larger
else if (photoBounds.height > photoBounds.width) {
NSLog(#"portrait height");
_photoImgView.frame = CGRectMake(0, 0, newWidth, scrollBounds.height);
frameToCenter.origin.x = xDist;
}
//Square
else {
NSLog(#"portrait square");
if ((statusbar == 1) || (statusbar == 2)) {
_photoImgView.frame = CGRectMake(0, 0, scrollBounds.width, newHeight);
frameToCenter.origin.y = yDist;
} else {
_photoImgView.frame = CGRectMake(0, 0, newWidth, scrollBounds.height);
frameToCenter.origin.x = xDist;
}
}
}
//Rotation Magic
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//later
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self photoBounds];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
//Zoom Ability
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.photoImgView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
NSLog(#"scale %f", scale);
NSLog(#"done zooming");
}
//Touches Control
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
//CGPoint touchPoint=[gesture locationInView:_scrollView];
NSLog(#"touched");
NSLog(#"single touch");
[self performSelector:#selector(callingHome) withObject:nil afterDelay:0];
}
- (void)dTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
NSLog(#"double touched");
}
- (void)panGestureCaptured:(UIPanGestureRecognizer *)gesture
{
NSLog(#"pan gesture");
}
- (void)callingHome {}
#end
The overall issue is that I can not get my picture to display correctly and be able to zoom on just it, no space around it and It needs to be the correct dimensions on load. I've been struggling with it for a few days.
Any help?
I have resolved a similar problem using a really simple subclass of UIScrollView of my own. You can take a look at it here - https://gist.github.com/ShadeApps/5a29e1cea3e1dc3df8c8. Works like charm for me.
That's how you init it:
scrollViewMain.delegate = self;
scrollViewMain.minimumZoomScale = 1.0;
scrollViewMain.maximumZoomScale = 3.0;
scrollViewMain.contentSize = imageViewMain.frame.size;
scrollViewMain.backgroundColor = [UIColor blackColor];
scrollViewMain.tileContainerView = imageViewMain;
There is awesome blog post about this problem from Peter Steinberger: http://petersteinberger.com/blog/2013/how-to-center-uiscrollview/

Resources