Question

I have an object created from a class of type UIView to produce an unlimited number of views which then appear in smaller sizes in the middle of the screen. I used UITapGestureRecognizer to enlarge one of the views to fit the screen but the issue here is that the object is only usable within the first class and cannot be transferred to the second class which is the selector. What are your suggestions regarding this particular issue? How can I send my object to the selector to be able to use it there? Thank you

Here is the first class which creates the object (derived from 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;
}

And the selector:

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

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

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

}

So instead of using "carousel" which represents all the views, I want to use "view" so that I can only enlarge the current view and not all of them.

Was it helpful?

Solution

UITapGestureRecognizer inherits the view property from UIGestureRecognizer, which you can use to enlarge just that tap gesture recognizer's view:

- (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;
}

Also, CGAffineTransformMakeScale(1, 1) will scale the view to its original size, so I bumped the scale up in my code snippet.

OTHER TIPS

If I'm understanding this correctly you want to transform the UIView that you tapped on, which should be the carousel?

Assuming you added the tap gesture recognizer to self.view, then this is how you get the view you just tapped on:

- (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
}

Once you know whether or not you tapped on the carousel, you can store it, pass it on to another class, transform it, or anything you like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top