-(void)initialization
UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)] autorelease];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[imageview addGestureRecognizer:panRecognizer];
}
- (void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = imageview;
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint translatedCenter = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
CGPoint center = [self centerWithBounds:translatedCenter andViewFrame:[piece frame] andBoundingFrame:[[piece superview] frame]];
[piece setCenter:center];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
I add the gesture to the imageview its not calling move action.
how to add the gesture to only UIImageView ..
Try setting imageview.userInteractionEnabled = YES. UIImageViews has userInteractionEnabled set to NO by default.
Related
I have a photo on the screen which when held, I want to display at full size. Here's the gesture recognizer:
UILongPressGestureRecognizer *hold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(hold:)];
hold.minimumPressDuration = 0;
[self.photoImageView addGestureRecognizer:hold];
And here's the listener:
-(void)hold:(UILongPressGestureRecognizer *)sender{
UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
img.userInteractionEnabled = NO;
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(#"state began");
[self.view addSubview:img];
} else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(#"state ended");
[img removeFromSuperview];
}
}
The image gets added to the screen correctly, however [img removeFromSuperview] does not seem to respond. Any reason for this happening? The NSLog for ending state is correctly firing off.
It's because you are creating a new instance of UIImageView instead of removing the one that is displayed.
-(void)hold:(UILongPressGestureRecognizer *)sender{
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(#"state began");
UIImageView *img = [[UIImageView alloc] initWithImage:self.photo];
img.tag = IMAGE_VIEW_TAG;
img.userInteractionEnabled = NO;
[self.view addSubview:img];
} else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(#"state ended");
UIImageView *img = [self.view viewWithTag: IMAGE_VIEW_TAG];
[img removeFromSuperview];
}
}
You don't need to create new imageview in gesture's action. Just add or remove your self.photoImageView like this
-(void)hold:(UILongPressGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
NSLog(#"state began");
[self.view addSubview:self.photoImageView];
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
NSLog(#"state ended");
[self.photoImageView removeFromSuperview];
}
}
I want the user to be able to pan from each thumb simultaneously but I can't figure out how to detect it with uigesturerecognizer. I can detect a tap and a pan simultaneously no problem.
It appears that the second pan will block the first.
Any help is appreciated.
I solved it by defining shouldReceiveTouch like so:
-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if (gestureRecognizer == singleTap) {
return YES;
}
if (gestureRecognizer == pan1 && [touch locationInView:self].x > 160) {
return YES;
}
if (gestureRecognizer == pan2 && [touch locationInView:self].x <= 160) {
return YES;
}
return FALSE;
}
And initWithFrame has the following code:
self.userInteractionEnabled = YES;
singleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
singleTap.numberOfTapsRequired = 1;
[self addGestureRecognizer:singleTap];
[singleTap release];
NSLog(#"tap: %p", singleTap);
pan1 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan1:)];
[self addGestureRecognizer:pan1];
NSLog(#"pan1: %p", pan1);
pan2 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePan2:)];
[self addGestureRecognizer:pan2];
for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
recognizer.delegate = self;
}
NSLog(#"pan2: %p", pan2);
When my hitView comes partially from the superview after a UIPanGestureRecognizer, the UILongPressGestureRecognizer don't work. Why?
- (id)initWithFrame:(CGRect)frame
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setDelegate:self];
[_glassesImage addGestureRecognizer:panGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(enchance:)];
longPressGesture.minimumPressDuration = 0.2;
[_glassesImage addGestureRecognizer:longPressGesture];
[self addSubview:_glassesImage];
}
- (void)enchance:(UILongPressGestureRecognizer *)gestureRecognizer
{
UIView *hitView = [gestureRecognizer view];
hitView.alpha=0.6;
inLongPress=YES;
gestureRecognizer.allowableMovement = 200;
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded){
hitView.alpha=1.0;
inLongPress=NO;
}
}
Try defining a delegate for your gesture recognizers and then provide an implementation for:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
(refs)
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
Add delegate to longPressGesture.
longPressGesture.delegate = self;
I think it will be helpful to you.
based on 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 got around of checking CGRectIntersectsRect single uiimageview. how will i need to check multiple uiimageview?
CORRECT
WRONG
-(void)initImagesAndGesture
{
UIImage *img = [UIImage imageNamed:#"beer.png"];
imgView1 = [[UIImageView alloc]initWithImage:img];
[imgView1 setFrame:CGRectMake(0, 0, 75, 115)];
[imgView1 setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView1 addGestureRecognizer:recognizer1];
[self.view addSubview:imgView1];
img = [UIImage imageNamed:#"cups.png"];
imgView2 = [[UIImageView alloc]initWithImage:img];
[imgView2 setFrame:CGRectMake(200, 240, 64, 75)];
[imgView2 setUserInteractionEnabled:YES];
recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView2 addGestureRecognizer:recognizer1];
[self.view addSubview:imgView2];
}
-(void)handlePan1:(UIPanGestureRecognizer *)recognizer
{
if (!(CGRectIntersectsRect(imgView1.frame, imgView2.frame)))
{
CGPoint pre_moveLocation = [recognizer locationInView:recognizer.view];
NSLog(#"previous location %#",NSStringFromCGPoint(pre_moveLocation));
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:recognizer.view];
}
else
{
NSLog(#"intersect!");
CGPoint pre_moveLocation = [recognizer locationInView:recognizer.view];
NSLog(#"previous location %#",NSStringFromCGPoint(pre_moveLocation));
recognizer.view.center = CGPointMake(pre_moveLocation.x, pre_moveLocation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view];
//[imgView2 setFrame:CGRectMake(200, 240, 64, 75)];
//[imgView1 setFrame:CGRectMake(0, 0, 75, 115)];
}
}
-(void)addImgView:(UIPanGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[imgView setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView addGestureRecognizer:recognizer1];
[self.view addSubview:imgView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initImagesAndGesture];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addImgView:)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:tapRecognizer];
}
Just iterate through all subviews...
// targetView is the one you are checking...
- (BOOL)isViewIntersectingAnyOtherViews:(UIView*)targetView {
for (UIView *view in self.view.subviews) {
if (view == targetView) continue; // Don't check against your own view
if (CGRectIntersectsRect(view, thisView)) {
return YES;
}
}
return NO;
}
I have a Background view (UIImageView) in that i am adding a view(UIView).
this contentView(UIView) contains UIPanGestureRecognizer.
I am giving UIPanGestureRecognizer to contentView by bellow code :
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(movePhoto:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[contentView addGestureRecognizer:panRecognizer];
[panRecognizer release];
and then i am adding this contentView to Background View (UIImageView) by bellow code :
[imgBG addSubview:contentView]; // here imgBG is Background view.
but when i move this contentView it goes out of imgBG.
bellow is the method to move contentView
[[[(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];
CGFloat finalX = [sender view].center.x;
CGFloat finalY = [sender view].center.y;
[[sender view] setCenter:CGPointMake(finalX, finalY)];
I think, here the contentView should remain inside the imgBG as it is the subview of imgBG.
Please let me know whether there is a problem in my code or its due to UIPanGestureRecognizer.
thanks in advance.
I just had to do :
[imgBG setClipsToBounds:YES];