0

I am trying to rotate the custom view added to the main view. I have tried to add the gesture recognizer to both main view and sub view. Here is my code

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *demoView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    UIRotationGestureRecognizer *rg= [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    [rg setDelegate:self];
    self.view.userInteractionEnabled = YES;
    demoView.userInteractionEnabled = YES;
    [demoView addGestureRecognizer:rg];
    [self.view addGestureRecognizer:rg];
    demoView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:demoView];
}

-(IBAction)handleTap:(UIRotationGestureRecognizer *)recognizer
{
    //NSLog(@"HERE");
    recognizer.view.transform = CGAffineTransformMakeRotation([recognizer rotation]);
    recognizer.rotation = 0;
    //NSLog(@"AND HERE");
}
TryinHard
  • 4,078
  • 3
  • 28
  • 54

1 Answers1

0

the problem is that one geturerecognizer can only ever be attached to one view. for your requirements, you need two gesturerecognizers, but you can make them call the same selector, so theres no need to implement it two times.

EDIT: Its kinda hard to find this stated clearly in the docs, but considering that the view- property is a single view, and not an array or set or whatever, it makes alot of sense. Heres the docs: https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/instp/UIGestureRecognizer/view

Next EDIT

This stackoverflow-answer also illustrates my point: Can you attach a UIGestureRecognizer to multiple views? so whoever downvoted my answer, at least tell me why.

Community
  • 1
  • 1
katzenhut
  • 1,742
  • 18
  • 26