Reputation: 524
I am trying to apply gesture on a UIImageView when iam giving action for the gesture scaled rotating and move of image in imageview working. so i can see image rotating and scaled and move from one point to another point.
When i scaled the image and zoom level to minimum and on touching on UIView which is super class for the UIImageView that too giving the gesture action. How to restrict the gesture which need to work only on UIImageView not on UIView. and trying to restrict it inside UIVIew the Imageview need not get outside the view.
-(void)InitGestures{
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapRecognizer.numberOfTouchesRequired = 1;
templatePhotoPlaceholderView=[[UIView alloc]init];
templatePhotoPlaceholderView.clipsToBounds = YES;
//templatephotoplaceholder frame setting
[self templatePhotoPlaceholderFrameSetting];
templatePhotoPlaceholderView.backgroundColor=[UIColor colorWithRed:0.8823 green:0.8823 blue:0.8823 alpha:1];
[self photoView:templatePhotoPlaceholderView];
tapRecognizer.view.frame=templatePhotoPlaceholderView.frame;
[self photoButtonPlaceHolder:templatePhotoPlaceholderView];
[selectedTemplateImage addSubview:templatePhotoPlaceholderView];
[templatePhotoPlaceholderView addGestureRecognizer:tapRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[templatePhotoPlaceholderView addGestureRecognizer:panRecognizer];
//[panRecognizer release];
UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease];
[pinchRecognizer setDelegate:self];
[self.view addGestureRecognizer:pinchRecognizer];
//[pinchRecognizer release];
[self.view addSubview:templatePhotoPlaceholderView];
[tapRecognizer release];
}
-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint translatedPoint = [gestureRecognizer translationInView:templatePhotoPlaceholderView];
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
_firstX = [imageview center].x;
_firstY = [imageview center].y;
}
translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
[imageview setCenter:translatedPoint];
}
Upvotes: 3
Views: 575
Reputation: 1203
First you add UIImageView in UIView. and apply Gesture on each UIView. and change the image of UIImageView. so it look like rotation. i m attacking following code. try that. This sample is for 3 UIView in horizontal.
-(void)addSwipeEvent:(UIView*)subView{
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[subView addGestureRecognizer:recognizer];
[recognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[subView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
downRecognizer.direction=UISwipeGestureRecognizerDirectionDown;
downRecognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[subView addGestureRecognizer:downRecognizer];
[downRecognizer release];
UISwipeGestureRecognizer *upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
upRecognizer.direction=UISwipeGestureRecognizerDirectionUp;
upRecognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[subView addGestureRecognizer:upRecognizer];
[upRecognizer release];
}
- (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
if ( sender.direction == UISwipeGestureRecognizerDirectionLeft ){
NSLog(@" *** SWIPE LEFT ***");
}
if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){
NSLog(@" *** SWIPE RIGHT ***");
}
if ( sender.direction== UISwipeGestureRecognizerDirectionUp ){
NSLog(@" *** SWIPE UP ***");
}
if ( sender.direction == UISwipeGestureRecognizerDirectionDown ){
NSLog(@" *** SWIPE DOWN ***");
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch.view isKindOfClass:[UIView class]])
{
viewIndex = touch.view.tag;
return YES;
}
return NO;
}
-(void)lowLevelSwipeRecognizer{
CATransition *myTransition = [CATransition animation];
myTransition.type = kCATransitionPush;
myTransition.duration = 0.3;
myTransition.subtype = kCATransitionFromRight;
NSString *str = [arrayImg objectAtIndex:0];
for (int i=0; i<2; i++) {
[arrayImg replaceObjectAtIndex:i withObject:[arrayImg objectAtIndex:i+1]];
}
[arrayImg replaceObjectAtIndex:2 withObject:str];
for (int cnt = 0; cnt<3; cnt++) {
if ([[arrayView objectAtIndex:cnt] isKindOfClass:[CustomView class]]) {
CustomView *v = (CustomView *)[arrayView objectAtIndex:cnt];
[v.imageView setImage:[UIImage imageNamed:[arrayImg objectAtIndex:cnt]]];
[v.layer addAnimation:myTransition forKey:nil];
}
}
}
Upvotes: 1
Reputation: 999
The following code restrict the moving image within the superview bounds
if (recognizer.state == UIGestureRecognizerStateBegan)
{
previousPoint = [recognizer translationInView:self];
}
else if (recognizer.state == UIGestureRecognizerStateChanged)
{
CGRect parentFrame = self.frame;
CGRect currentFrame = imageView.frame;
if (CGRectContainsRect(parentFrame, currentFrame))
{
CGPoint point = [recognizer translationInView:self.superview];
CGPoint movePoint = CGPointMake(point.x - previousPoint.x, point.y - previousPoint.y);
CGPoint newCenter = CGPointMake(imageView.center.x + movePoint.x, imageView.center.y + movePoint.y);
imageView.center = newCenter;
previousPoint = point;
}
else
{
CGRect imageRect = imageView.frame;
if (imageRect.origin.x < 0)
imageRect.origin.x = 0;
else if (imageRect.origin.x + imageRect.size.width >= self.frame.size.width)
imageRect.origin.x = self.frame.size.width - imageRect.size.width;
if (imageRect.origin.y < 0)
imageRect.origin.y = 0;
else if (imageRect.origin.y + imageRect.size.height >= self.frame.size.height)
imageRect.origin.y = self.frame.size.height - imageRect.size.height;
imageView.frame = imageRect;
}
}
Upvotes: 0