0

我正在尝试旋转添加到主视图的自定义视图。我试图将手势识别器添加到主视图和子视图。这是我的代码

- (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");
}
4

1 回答 1

0

问题是一个 geturerecognizer 只能附加到一个视图。根据您的要求,您需要两个手势识别器,但您可以让它们调用同一个选择器,因此无需执行两次。

编辑:很难在文档中明确说明这一点,但考虑到view- 属性是单个视图,而不是数组或集合或其他任何东西,这很有意义。继承人的文档: https ://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/instp/UIGestureRecognizer/view

下一个编辑

这个 stackoverflow-answer 也说明了我的观点: 你可以将 UIGestureRecognizer 附加到多个视图吗? 所以无论谁拒绝我的回答,至少告诉我为什么。

于 2013-10-14T10:43:04.750 回答