有没有办法通过UITapGestureRecognizer发送对象?

时间:2014-02-20 17:17:24

标签: ios uiview uitapgesturerecognizer

我有一个从UIView类型创建的对象,可生成无限数量的视图,然后在屏幕中间以较小的尺寸显示。我使用UITapGestureRecognizer来放大其中一个视图以适应屏幕,但这里的问题是该对象仅在第一个类中可用,并且不能转移到作为选择器的第二个类。 您对此特定问题有何建议? 如何将我的对象发送到选择器以便能够在那里使用它? 谢谢

这是创建对象的第一个类(从iCarousel派生):

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    UIButton *close = [UIButton buttonWithType:UIButtonTypeRoundedRect];


    //create new view if no view is available for recycling
    if (view == nil)
    {


        view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 460.0f)];
        view.contentMode = UIViewContentModeCenter;

        view.backgroundColor = [UIColor whiteColor];

        label = [[UILabel alloc] initWithFrame:CGRectMake(100, -100, 100, 100)];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [label.font fontWithSize:50];
        label.tag = 1;
        [view addSubview:label];

        close.frame = CGRectMake(0, 0, 30, 30);
        [close setTitle:@"x" forState:UIControlStateNormal];
        close.titleLabel.font = [UIFont systemFontOfSize:25];
        [close addTarget:self action:@selector(closeMe:) forControlEvents:UIControlEventTouchUpInside];

        [view addSubview:close];


        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToMaximize:)];
        tap.numberOfTapsRequired = 1;
        [view addGestureRecognizer:tap];

    }
    else
    {

        //get a reference to the label in the recycled view
        label = (UILabel *)[view viewWithTag:1];

    }


    label.text = [items[index] stringValue];

    return view;
}

选择器:

- (void)tapToMaximize:(UITapGestureRecognizer*)recognizer {

    [UIView animateWithDuration:0.3
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         carousel.transform = CGAffineTransformMakeScale(1, 1);

                     }
                     completion:^(BOOL finished) {
                     }];

}

因此,我不想使用代表所有视图的“carousel”,而是使用“view”,这样我只能放大当前视图,而不是全部视图。

2 个答案:

答案 0 :(得分:0)

UITapGestureRecognizer继承了view的{​​{1}}属性,您可以使用它来放大该手势识别器的视图:

UIGestureRecognizer

此外,- (void)tapToMaximize:(UITapGestureRecognizer*)recognizer { UIView *viewToMaximize; if ([recognizer.view isEqual:_maximizedView]) { viewToMaximize = nil; } else { viewToMaximize = recognizer.view; } UIView *viewToUnmaximize = _maximizedView; // _maximizedView should be an instance variable on your view controller [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ viewToMaximize.transform = CGAffineTransformMakeScale(1.1, 1.1); viewToUnmaximize.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { }]; _maximizedView = viewToMaximize; } 会将视图缩放到原始大小,因此我在代码段中提升了比例。

答案 1 :(得分:0)

如果我正确地理解了这一点,你想要改变你所使用的UIView,它应该是旋转木马吗?

假设您已将点击手势识别器添加到self.view,那么您就可以获得刚刚点击的视图:

- (void)tapToMaximize:(UITapGestureRecognizer*)recognizer
{
    UIView *view = [self.view hitTest:[recognizer locationInView:self.view] withEvent:nil];
    // you can now check to see if this view is the carousel
}

一旦你知道你是否点击了旋转木马,就可以存储它,将它传递给另一个类,转换它或任何你喜欢的东西。