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.
Related
Okay I'm lost here.
So have this UIViewController we can call it viewController1 where I add a new editable UITextView for every click on an add button.
I then use UIPanGestureRecognizer so you can drag them where ever you want. This works just fine. Now I want to go to viewController2: [self.navigationController pushViewController:viewController2 animated:YES]; but when I press the back button to return to viewController1 the first UITextView that was created in viewController1 has changed. It's not gone but the text property inside the textview has moved.
This just happens for the first instance of UITextView that I create, any ideas here would be appreciated.
Here's my method for creating the text:
-(void)addTextFieldToScreen
{
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 140, 120)];
textView.text = #"TEXT";
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor purpleColor];
textView.font = [UIFont fontWithName:#"AmericanTypewriter" size:30];
textView.userInteractionEnabled = YES;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveText:)];
panRecognizer.minimumNumberOfTouches = 1;
panRecognizer.maximumNumberOfTouches = 1;
panRecognizer.delegate = self;
[textView addGestureRecognizer:panRecognizer];
[self.view addSubview:textView];
}
And here's my method for making my textView dragable:
-(void)moveText:(id)sender {
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view 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 + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat workingWidth = screenRect.size.width - 40;
CGFloat workingHeight = screenRect.size.height - 80;
if(finalX < 50) {
finalX = 50;
}
else if(finalX > workingWidth ) {
finalX = workingWidth;
}
if(finalY < 80) {
finalY = 80;
}
else if(finalY > workingHeight) {
finalY = workingHeight;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:.20];
[UIView commitAnimations];
}
}
EDIT:
Okay here's whats going on, I add two textviews with content "TEXT" with the "add text" button in the right corner in viewController1:
And then when I come back from viewController2 to viewController1 this is what I see:
As you can see it's only the left textView instance text property that change position, and this is the first one I'm adding.
Hi. I am beginner in cocos2d. I want to pan an image (not rotate) like
.
Please any one suggest me.
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)]; // calling a method **move**
[panRecognizer setMinimumNumberOfTouches:1]; // set yourself
[panRecognizer setMaximumNumberOfTouches:2]; //
[panRecognizer setDelegate:self];
[yourImage addGestureRecognizer:panRecognizer];
/*
CGFloat firstX;
CGFloat firstY;
*/
//---method move---//
-(IBAction)move:(id)sender
{
yourImage.clipsToBounds = YES;
[yourImage 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:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
I suggest UIPanGestureRecognizer. You can find such a beautiful tutorial on how to do this at RayWenderlich's Article here. Hope this helps.
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 ..
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[templatePhotoPlaceholderView addGestureRecognizer:panRecognizer];
-(void)move:(UIPanGestureRecognizer *)gestureRecognizer{
CGPoint translatedPoint = [gestureRecognizer translationInView:imageview];
CGPoint center;
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
_firstX = [imageview center].x;
_firstY = [imageview center].y;
if(center.x < templatePhotoPlaceholderView.frame.origin.x + (imageview.frame.size.width/2)){
center.x = templatePhotoPlaceholderView.frame.origin.x+(imageview.frame.size.width/2);
}
if(center.x > templatePhotoPlaceholderView.frame.origin.x+templatePhotoPlaceholderView.frame.size.width - (imageview.frame.size.width/2)){
center.x = templatePhotoPlaceholderView.frame.origin.x+templatePhotoPlaceholderView.frame.size.width - (imageview.frame.size.width/2);
}
if(center.y < templatePhotoPlaceholderView.frame.origin.y + (imageview.frame.size.height/2)){
center.y = templatePhotoPlaceholderView.frame.origin.y + (imageview.frame.size.height/2);
}
if(center.y > templatePhotoPlaceholderView.frame.origin.y + templatePhotoPlaceholderView.frame.size.height -(imageview.frame.size.height/2)){
center.y = templatePhotoPlaceholderView.frame.origin.y + (templatePhotoPlaceholderView.frame.size.height)-(imageview.frame.size.height/2);
}
}
translatedPoint = CGPointMake(center.x+translatedPoint.x, center.y+translatedPoint.y);
[imageview setCenter:translatedPoint];
}
how to move an image in a specified view [templatePhotoPlaceholderView is a view imageview is an UIImageView]
When the image is touches the edges of all corner of UIView then image need not to move.
Not to allow the image to go out side of the UIView.
i try but not able to fix the image view in specified region to be moved.
# sorry if any grammatical mistake in typing.
#all please advice me how to figure the issue.
// Scaling
UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)] autorelease];
[pinchRecognizer setDelegate:self];
[templatePhotoPlaceholderView addGestureRecognizer:pinchRecognizer];
[self.view addSubview:templatePhotoPlaceholderView];
[tapRecognizer release];
- (void)scale:(UIPinchGestureRecognizer *)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
float hfactor = imageview.frame.size.width / templatePhotoPlaceholderView.frame.size.width;
float vfactor = imageview.frame.size.height / templatePhotoPlaceholderView.frame.size.height;
float factor = MAX(hfactor, vfactor);
if (([gestureRecognizer scale] > 1 && factor < 3) || ([gestureRecognizer scale] <= 1 && factor >1) ) {
imageview.transform = CGAffineTransformScale([imageview transform], [gestureRecognizer scale], [gestureRecognizer scale]);
}
[gestureRecognizer setScale:1];
}
}
If you are not scaling the image or view then it's very simple.
try this way:
-(void)move:(UIPanGestureRecognizer *)gestureRecognizer{
CGPoint translatedPoint = [gestureRecognizer translationInView:imageview];
CGPoint center;
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
_firstX = [imageview center].x;
_firstY = [imageview center].y;
//checking for x axis
if((self.view.frame.origin.x + 2 > _firstX) && (imageVIew.frame.size.width+_fisrtX < 340))
//perform move condition
//checking for Y axis
if(self.view.frame.origin.y+ 2 > _firstY) && (imageview.Frame.size.height + _firstY < 420)
//perform move condtion
}
I want to rotate the uiimageview also scale the image view so how do i detect touch gesture that user is scaling or user is rotating?
Beginning in iOS 3.2, Apple introduced the use of UIGestureRecognizer's. The two that you would be interested in are UIPinchGestureRecognizer and UIRotationGestureRecognizer. To use a pinch gesture you could do the following:
- (void)viewDidLoad {
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchGesture:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
}
- (void)handlePinchGesture:(UIGestureRecognizer *)sender {
NSLog(#"New Scale: %f", sender.scale);
}
You could do something similar for the UIRotationGestureRecognizer then.
in your viewcontroller.h file write down this properties
IBOutlet UIImageView *imgRotate;
CGFloat lastScale;
CGFloat lastRotation;
now in your viewcontroller.m file write the below code,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[imgRotate addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[imgRotate addGestureRecognizer:rotationRecognizer];
}
-(void)scale:(id)sender
{
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];
[imgRotate setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender
{
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];
[imgRotate setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if ([touch view] == imgRotate) {
CGPoint location = [touch locationInView:self.view];
imgRotate.center = location;
}
}