Image position on view iOS - iphone

I have some problem with image dragging. So I use this code:
-(void)panDetected:(UIPanGestureRecognizer *)panRecognizer {
CGPoint translation = [panRecognizer translationInView:self.view];
CGPoint imageViewPosition = player1.center;
imageViewPosition.x +=translation.x;
imageViewPosition.y +=translation.y;
player1.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView:self.view];
[self SaveColorRealTime];
}
-(void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer {
CGFloat scale = pinchRecognizer.scale;
player1.transform = CGAffineTransformScale(player1.transform, scale, scale);
pinchRecognizer.scale = 1.0;
}
-(void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer {
CGFloat angle = rotationRecognizer.rotation;
player1.transform = CGAffineTransformRotate(player1.transform, angle);
rotationRecognizer.rotation = 0.0;
}
-(void)tapDetected:(UITapGestureRecognizer *)tapRecognizer {
[UIView animateWithDuration:0.25 animations:^{
player1.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
player1.transform = CGAffineTransformIdentity;
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
player1.userInteractionEnabled = YES;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
[player1 addGestureRecognizer:panRecognizer];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchDetected:)];
[player1 addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:#selector(rotationDetected:)];
[player1 addGestureRecognizer:rotationRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 2;
[player1 addGestureRecognizer:tapRecognizer];
It works, user can drag image on view, but when user go to other view, position of image is refresh.
Have you any ideas how can I save image position?
Thanks

Related

When Rotating UIImageView The Image Moves Off Screen/Out of View

I've got a view on screen with a UIImageView inside it. The user can pan, scale, and rotate.
I have a rotation gesture setup, and everything is fine with rotation except for when rotating the image moves off the screen/out of the view.
Any idea what would cause this to behavior? The expected behavior I'm looking for is when you rotate the image it does not move at all (other than rotating).
I've tried setting an anchor point of the layer of the parent view, but it doesn't seem to be helping any.
EDIT: I've put some logging on the rotation gesture and what's happening is the image views center (X) is what is getting off hence it going off the screen.
Why would this coordinate change upon rotation? The Y is staying the same. I've even tried turning the other gestures off and it's definitely something whacky with the rotation gesture.
Code Below:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CALayer *l = [self.viewCase layer];
[l setMasksToBounds:YES];
[l setCornerRadius:30.0];
self.imgUserPhoto.userInteractionEnabled = YES;
[self.imgUserPhoto setClipsToBounds:NO];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panDetected:)];
panRecognizer.maximumNumberOfTouches = 1;
[self.view addGestureRecognizer:panRecognizer];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinchDetected:)];
[self.view addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotationDetected:)];
[self.view addGestureRecognizer:rotationRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapRecognizer];
panRecognizer.delegate = self;
pinchRecognizer.delegate = self;
rotationRecognizer.delegate = self;
}
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.view];
CGPoint imageViewPosition = self.imageView.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;
self.imageView.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView:self.view];
}
- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
CGFloat scale = pinchRecognizer.scale;
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);
pinchRecognizer.scale = 1.0;
}
- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer
{
CGFloat angle = rotationRecognizer.rotation;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, angle);
rotationRecognizer.rotation = 0.0;
}
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
[UIView animateWithDuration:0.25 animations:^{
self.imageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
self.imageView.transform = CGAffineTransformIdentity;
}];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Maybe because the pan gesture is also getting called : try setting the maximumNumberOfTouches to 1 on your UIPanGestureRecognizer

How to make collage in shape for iPhone application?

I want many collages in some particular shape like heart shape, star shape, etc...
Can any one tell me how can I do this programmatically in my iPhone app with objective c?
Use this code. Here The images are being picked from the PhotoLibrary and then using gestures they are being moved, zoomed etc. I am using All the gestures to help u do virtually any panning, pinching and rotating of any image you put.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
imageCollage = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 150, 150)];
[imageCollage setUserInteractionEnabled:YES];
imageCollage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,imageCollage.frame .size.width, imageCollage.frame.size.height)];
holderView.layer.cornerRadius = 6;
holderView.clipsToBounds = YES;
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageCollage.image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
DeleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[DeleteImage setImage:[UIImage imageNamed:#"DeleteRed.png"]];
[holderView addSubview:DeleteImage];
DeleteImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[DeleteImage addGestureRecognizer:tapRecognizer];
[BaseView addSubview:holderView];
[Trash setImage:[UIImage imageNamed:#"12456977871712665073hrum_trash.svg.med.png"] forState:UIControlStateNormal];
}
-(void)scale:(id)sender
{
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender
{
[BaseView bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)move:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 260) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 416) {
finalY = 416;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 416) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 260) {
finalY = 416;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
-(void)tapped:(UIGestureRecognizer *)recogniser
{
NSLog(#"%#",recogniser.view);
[Trash setImage:[UIImage imageNamed:#"trash_bin_recycle_quit_terminate_error_cancel_close_exit.png"] forState:UIControlStateNormal];
[[[recogniser view] superview] removeFromSuperview];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
Any queries about the cod please feel free to ask :)... Happy Coding ..
If you have little experience with Core Graphics and want to programmatically generate those kinds of shapes, I would recommend you use a Mac app called PaintCode.
You can use it to draw your shapes, and it generates the corresponding Objective-C drawing code.

How to increase or decrease the size of UIImageView by dragging the right corner of the edge?

I am doing an app which has a feature that resizing UIImageView by dragging the right corner of the UIImageView's edge like zoom in and zoom out. Also we can rotate the image.
How can I achieve this?
In case of resizing UIImage use SPUserResizableView follow link.
For rotation u will add UIRotationGestureRecognizer to resizableview.
Use this code. Here The images are being picked from the PhotoLibrary and then using gestures they are being moved, zoomed etc. I am using all the gestures to help u do virtually anything like panning, pinching and rotating of any image you put.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
imageCollage = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 150, 150)];
[imageCollage setUserInteractionEnabled:YES];
imageCollage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,imageCollage.frame .size.width, imageCollage.frame.size.height)];
holderView.layer.cornerRadius = 6;
holderView.clipsToBounds = YES;
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageCollage.image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
DeleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[DeleteImage setImage:[UIImage imageNamed:#"DeleteRed.png"]];
[holderView addSubview:DeleteImage];
DeleteImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[DeleteImage addGestureRecognizer:tapRecognizer];
[BaseView addSubview:holderView];
[Trash setImage:[UIImage imageNamed:#"12456977871712665073hrum_trash.svg.med.png"] forState:UIControlStateNormal];
}
-(void)scale:(id)sender
{
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender
{
[BaseView bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)move:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 260) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 416) {
finalY = 416;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 416) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 260) {
finalY = 416;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
-(void)tapped:(UIGestureRecognizer *)recogniser
{
NSLog(#"%#",recogniser.view);
[Trash setImage:[UIImage imageNamed:#"trash_bin_recycle_quit_terminate_error_cancel_close_exit.png"] forState:UIControlStateNormal];
[[[recogniser view] superview] removeFromSuperview];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
Any queries about the code please feel free to ask :)... Happy Coding ..

how to add on touch and detect UIImageView on view so i could move it around and not adding one on top of it?

i wonder if you guys have a sample code adding and move UIImageView on touch and also detect if there is an UIImageView there so i can't add UIImageView on top of it.
EDIT: clearer question
1. i wanna add an cups(UIImageView) when i touch the view but do not wan the cup(UIImageView) to stack.
i wanna move the UIImageView but will bounce back to the original position if there is an UIImageView there so it will not stack the UIImageView already there.
thanks for reading my question and appreciated your helps
cheers
des
You can add a UIPanGestureRecognizer to the UIImageView to handle the dragging, as seen below.
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageView addGestureRecognizer:panRecognizer];
Implement the following method:
-(void)move:(id)sender {
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;
    }
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
    [[sender view] setCenter:translatedPoint];
}
In combination to the above, you can use CGRectIntersectsRect to find if it's intersecting another cup.
BOOL isIntersecting = CGRectIntersectsRect ([cup1 frame],[cup2 frame]);
You would have to wrap the functionality to take into account all your cups but this gives you an idea a to how to accomplish what you're trying to do.
This should let you going…
Create two ivars in your header file:
UIImageView *myImageView1;
UIImageView *myImageView2;
The next two methods will be in your implementation file:
-(void)initImagesAndPanGesture
{
UIImage *img = [UIImage imageNamed:#"image1.png"];
myImageView1 = [[UIImageView alloc]initWithImage:img];
[myImageView1 setFrame:CGRectMake(300, 800, 100, 100)];
[myImageView1 setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[myImageView1 addGestureRecognizer:recognizer1];
[self.view addSubview:myImageView1];
img = [UIImage imageNamed:#"image2.png"];
myImageView2 = [[UIImageView alloc]initWithImage:img];
[myImageView2 setFrame:CGRectMake(500, 800, 100, 100)];
[myImageView2 setUserInteractionEnabled:YES];
recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[myImageView2 addGestureRecognizer:recognizer1];
[self.view addSubview:myImageView2];
}
-(void)handlePan1:(UIPanGestureRecognizer *)recognizer
{
if (!(CGRectIntersectsRect(myImageView1.frame, myImageView2.frame)))
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
else
{
NSLog(#"intersect!");
}
}

Pinch to take photo full screen like Reeder apps

Trying to come up with a method for doing the exact same thing the reeder apps creator does in his iphone/ipad apps with pinch-to-expand photos to full screen.
I have a uiimageview in a table cell that I want to transition to a full screen view on pinch open, or maybe double tap. Would like to use a similar animation as well.
Any tips would be appreciated!
Ok I managed to put this together myself. Not really sure how to use a transition method, but I needed to duplicate the view in the same location and then blow it up.
http://screencast.com/t/MLTuGkIYh
So in my cell that contains the big image I hook up both the pinch and tap gesture recognizers.
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)] autorelease];
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)] autorelease];
tapGesture.numberOfTapsRequired = 2;
self.imageView.userInteractionEnabled = YES;
self.imageView.multipleTouchEnabled = YES;
[self.imageView addGestureRecognizer:pinchGesture];
[self.imageView addGestureRecognizer:tapGesture];
[cell.contentView addSubview:self.imageView];
and then here's the rest of the code. Basically when I recognized the gesture (and for pinching, make sure its finished) I place the duplicate view in the same exact location (gained via the convertRect stuff), and then animate its frame and background color. When returning from it, I do the inverse.
- (void)handlePinchGesture:(id)sender
{
if (((UIPinchGestureRecognizer *)sender).state == UIGestureRecognizerStateEnded) {
if(((UIPinchGestureRecognizer *)sender).view == self.imageView)
{
if (((UIPinchGestureRecognizer *)sender).scale > 1) {
[self showFloorPlanFullScreen];
}
} else {
if (((UIPinchGestureRecognizer *)sender).scale < 1) {
[self closeFloorPlanFullScreen];
}
}
}
}
- (void)handleTap:(id)sender
{
if (((UITapGestureRecognizer *)sender).view == self.imageView) {
[self showFloorPlanFullScreen];
} else {
[self closeFloorPlanFullScreen];
}
}
- (void)showFloorPlanFullScreen
{
CGRect newRect = [self.imageView convertRect:self.imageView.bounds toView:[self.splitViewController.view superview]];
UIImage *image = self.imageView.image;
self.fullScreenImageView = [[[UIImageView alloc] initWithImage:image] autorelease];
UIPinchGestureRecognizer *pinchGesture = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)] autorelease];
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)] autorelease];
tapGesture.numberOfTapsRequired = 2;
self.fullScreenImageView.userInteractionEnabled = YES;
self.fullScreenImageView.multipleTouchEnabled = YES;
[self.fullScreenImageView addGestureRecognizer:pinchGesture];
[self.fullScreenImageView addGestureRecognizer:tapGesture];
self.fullScreenImageView.contentMode = UIViewContentModeScaleAspectFit;
self.fullScreenImageView.frame = newRect;
self.fullScreenImageView.backgroundColor = [UIColor clearColor];
[[self.splitViewController.view superview] addSubview:self.fullScreenImageView];
CGRect splitViewRect = self.splitViewController.view.frame;
[UIView animateWithDuration:0.5 animations:^{
self.fullScreenImageView.backgroundColor = [UIColor blackColor];
self.fullScreenImageView.frame = splitViewRect;
}];
}
- (void)closeFloorPlanFullScreen
{
CGRect newRect = [self.imageView convertRect:self.imageView.bounds toView:[self.splitViewController.view superview]];
[UIView animateWithDuration:0.5
animations:^{
self.fullScreenImageView.backgroundColor = [UIColor clearColor];
self.fullScreenImageView.frame = newRect;
}
completion:^(BOOL finished) {
[self.fullScreenImageView removeFromSuperview];
self.fullScreenImageView = nil;
}];
}
If you want the picture to actual resize while zooming, I would recommend adding the duplicate view as soon as the pinching starts (and as long as its scaling > 1) and then apply the transformation:
CGAffineTransform myTransformation = CGAffineTransformMakeScale(((UIPinchGestureRecognizer *)sender).scale, ((UIPinchGestureRecognizer *)sender).scale);
self.fullScreenImageView.transform = myTransformation;
As soon as the pinching hits a end state, I would then fade in the black and adjust the frame. I decided not to go with this method as I think just recognizing the pinch out or double tap is good enough.
You have to embed your UIImageView in an UIControl and link an IBAction to UIControl events.
Use a UIPinchGestureRecognizer on the image view to recognize the pinch and the UIView transition methods to blow the image view up to full size.