I have set animation with UILabel. And I add UITapGestureRecognizer for UILabel. But, I can not tap on UILabel.
This is my code:
Add gesture for UILabel:
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapOnLabel:)];
gesture.numberOfTapsRequired = 1;
_lbAnimation.userInteractionEnabled = YES;
[_lbAnimation addGestureRecognizer:gesture];
Animation UILabel
[UIView animateKeyframesWithDuration:6 delay:0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseOut animations:^{
_lbAnimation.frame=CGRectMake(0-(rect.size.width), _lbAnimation.frame.origin.y, rect.size.width, _lbAnimation.frame.size.height);
} completion:^(BOOL finished)
{
_viewAnimation.hidden = YES;
}];
handleTapOnLabel function
- (void)handleTapOnLabel:(UITapGestureRecognizer *)tapGesture
{
CGPoint locationOfTouchInLabel = [tapGesture locationInView:tapGesture.view];
NSLog(#"_lbAnimation: %f : %f", locationOfTouchInLabel.x, locationOfTouchInLabel.y);
}
I don't know cause UILabel not tap.
Please help me.
In my iOS application, I use UITouchGestureRecognizer to drag/drop, zoom and rotate UIImageView. I also store the information on the last position of the ImageView to retrieve it when the app restarts.
That was working well with iOS7 but I have recently started to test with iOS8 and I'm facing some issues, especially with the zoom. Whenever the imageview has a non-0 angle in the rotation parameter, the zoom is not acting like expected and reduces the size of the image.
I don't understand why the OS change does that, here is my code:
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
[self loadAllImages];
for (GIFavorite *favorite in self.favorites){
UIImage *image = favorite.image;
ImageView *imageView = [[ImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageView.center = CGPointMake(favorite.x, favorite.y);
imageView.transform = CGAffineTransformMakeRotation(favorite.rotation);
imageView.transform = CGAffineTransformScale(imageView.transform, favorite.scale, favorite.scale);
imageView.image = image;
imageView.userInteractionEnabled = YES;
UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panRecognizer.delegate = self;
[imageView addGestureRecognizer:panRecognizer];
UIRotationGestureRecognizer * rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
rotationRecognizer.delegate = self;
[imageView addGestureRecognizer:rotationRecognizer];
UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
pinchRecognizer.delegate = self;
[imageView addGestureRecognizer:pinchRecognizer];
[self.view addSubview:imageView];
}
}
I guess the way I am doing the transforms is wrong because when I put the CGAffineTransformMakeRotation instruction after the CGAffineTransformScale instead of reducing the size it increases it.
Where am I doing things wrong?
Edit: the code for handleRotate
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
ImageView *imageView = recognizer.view;
CGFloat radians = atan2f(imageView.transform.b, imageView.transform.a);
favorite.rotation = radians;
recognizer.rotation = 0;
}
First, you need to make your view controller implements UIGestureRecognizerDelegate.
SWIFT 3 :-
func setUpGestures () {
let panGesture = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(gesture:)))
self.imageView.addGestureRecognizer(panGesture)
let rotationGesture = UIRotationGestureRecognizer(target: self, action:#selector(self.handleRotationGesture(gesture:)))
self.imageView.addGestureRecognizer(rotationGesture)
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.handlePinchGesture(gesture:)))
self.imageView.addGestureRecognizer(pinchGesture)
}
func handlePinchGesture(gesture: UIPinchGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIPinchGestureRecognizer")
gesture.view?.transform = (gesture.view?.transform)!.scaledBy(x: gesture.scale, y: gesture.scale)
gesture.scale = 1.0
}
}
func handleRotationGesture(gesture: UIRotationGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIRotationGestureRecognizer")
gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation)
gesture.rotation = 0
}
}
func handlePanGesture(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIPanGestureRecognizer")
let translation = gesture.translation(in: view)
gesture.view?.transform = (gesture.view?.transform)!.translatedBy(x: translation.x, y: translation.y)
gesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
}
}
Hope, this is what you're looking for. Any concern get back to me. :)
First, you need to make your view controller implements UIGestureRecognizerDelegate.
Then add the gesture recognizers to your UIView (I see that you already did that, but just putting it here again to have the solution in one place).
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panGesture.delegate = self;
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
rotationGesture.delegate = self;
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
pinchGesture.delegate = self;
[yourView addGestureRecognizer:panGesture];
[yourView addGestureRecognizer:rotationGesture];
[yourView addGestureRecognizer:pinchGesture];
Then, this is how your handle... methods should be:
- (void)handlePinch:(UIPinchGestureRecognizer *)pinchRecognizer
{
UIGestureRecognizerState state = [pinchRecognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat scale = [pinchRecognizer scale];
[pinchRecognizer.view setTransform:CGAffineTransformScale(pinchRecognizer.view.transform, scale, scale)];
[pinchRecognizer setScale:1.0];
}
}
- (void)handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer {
UIGestureRecognizerState state = [rotationGestureRecognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat rotation = [rotationGestureRecognizer rotation];
[rotationGestureRecognizer.view setTransform:CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation)];
[rotationGestureRecognizer setRotation:0];
}
}
- (void)handlePan:(UIPanGestureRecognizer *)panRecognizer {
UIGestureRecognizerState state = [panRecognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [panRecognizer translationInView:panRecognizer.view];
[panRecognizer.view setTransform:CGAffineTransformTranslate(panRecognizer.view.transform, translation.x, translation.y)];
[panRecognizer setTranslation:CGPointZero inView:panRecognizer.view];
}
}
And this is how it looks:
try doing these..
first in your ViewControllers .h do these..
#interface RotationViewController : UIViewController<UIGestureRecognizerDelegate>//Add these..
{
UIView *testView;
}
now do these in .m file of your ViewController..
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
testView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 160, 160)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];
[imageView setImage:[UIImage imageNamed:#"ImageName.png"]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[testView addSubview:imageView];
[self.view addSubview:testView];
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
[testView addGestureRecognizer:rotationGestureRecognizer];
}
-(void) handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
testView.transform = CGAffineTransformRotate(testView.transform, rotationGestureRecognizer.rotation);
rotationGestureRecognizer.rotation = 0.0;
}
i hope it helps..
I have a UIScrollView .In that i have one ImageView.
I want to get UITOUCH point .
i know i can get location by
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}
But i want uitouch on image.
PLease help me.
try like this,
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: tapRecognizer.view]
}
AND set image view userInteractionEnabled to YES
UIImageView *image = [[UIImageView alloc]init];
image.frame = CGRectMake(55, 30, 50, 40);
image.image = [UIImage imageNamed:#"Default.png"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
singleTap.numberOfTapsRequired = 1;
self.view.tag =10;
image.userInteractionEnabled = YES;
[image addGestureRecognizer:singleTap];
I have 2 imageview, and I want them to rotate when they are clicked. This is my code:
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapRecognizerMethod:)];
Then in the tapRecognizerMethod:
- (void)tapRecognizerMethod:(UITapGestureRecognizer *)tapRecognize
{
if (tapRecognize == tapRecognizer) // tapRecognizer is the first imageviews recognizer
{
if (tapRecognize.state == what should go here?
{
//do something
}
}
}
What should go after tapRecognize ==?
- (void)tapRecognizerMethod:(UITapGestureRecognizer *)tapRecognize{
if (tapRecognize == tapRecognizer){
if (tapRecognize.state == UIGestureRecognizerStateEnded){
UIImageView *imgView = (UIImageView*)[tapRecognize view];
//Now need to rotate the image
}
}
}
Have a look at the following link:
rotating Image
- (void)handleTap:(UITapGestureRecognizer *)tapRecognize
{
if (tapRecognize == tapRecognizer)
{
CGAffineTransform transform = CGAffineTransformRotate(lineImage.transform, 90);
[lineImage setTransform:transform];
}
}
and make sure you have enabled the uesr interaction of imageviews,
[imageView1 setUserInteractionEnabled:YES];
[imageView2 setUserInteractionEnabled:YES];
If you actually requires a flip rotation you better chose below swipe gestures.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleSwipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipeLeft];
[swipeLeft release];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleSwipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeRight];
[swipeRight release];
On handler method you give an animation for separate flip right and left
if(isActiveFlipLeft)
{
[UIView transitionWithView:imageSnap
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
imageSnap.backgroundColor = [UIColor colorWithRed:187.0/225.0 green:187.0/225.0 blue:187.0/225.0 alpha:1.0];
imageSnap.image = image;
} completion:NULL];}
else if (isActiveFlipRight)
{
[UIView transitionWithView:imageSnap
duration:1.0f
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
imageSnap.backgroundColor = [UIColor colorWithRed:187.0/225.0 green:187.0/225.0 blue:187.0/225.0 alpha:1.0];
imageSnap.image = image;
} completion:NULL];}
The below code is for just a simple tap gesture. you will get call on handleSingleTap:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
[singleTap release];
Gesture handler
- (void)handleSwipeLeft:(UIGestureRecognizer *)gestureRecognizer;
- (void)handleSwipeRight:(UIGestureRecognizer *)gestureRecognizer;
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer;
If you requires real rotation you can use
imageview.transform = CGAffineTransformMakeRotation( 270.0/180*M_PI );
something like in your gesture handler
I'm showing a UIPopoverController in a ViewController, and I want to drag one image from the popover to the ViewController.
I know that to do it, I need to use a UIGestureRecognizer, but I don't know how. I can move the image around the popover, but I can't drag and drop it to the ViewController.
My code:
-(void)viewDidLoad
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panGesture];
}
- (void)moveImage:(UIPanGestureRecognizer *)recognizer
{
CGPoint newCenter = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan) {
beginX = ball.center.x;
beginY = ball.center.y;
}
newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);
[ball setCenter:newCenter];
}