user546459
user546459

Reputation: 569

UIScrollView with UITapGestureRecognizer and UIPageControl

I'm creating a horizontal scrolling tableview containing images. I have the swiping functionality working great, and am able to add the images to the scrollView as so in cellForRowAtIndexPath:

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
    [scrollView setContentSize:CGSizeMake(700, 78)];
    UIImage *footImage = [UIImage imageNamed:@"Foot.png"];
    UIImageView *footImageView = [[UIImageView alloc] initWithImage:footImage];
    footImageView.frame = CGRectMake(0, 0, 80, 78);
    footImageView.userInteractionEnabled = YES;
    [scrollView addSubview: footImageView];

    UIImage *handImage = [UIImage imageNamed:@"Hand.png"];
    UIImageView *handImageView = [[UIImageView alloc] initWithImage:handImage];
    handImageView.frame = CGRectMake(90, 0, 80, 78);
    handImageView.userInteractionEnabled = YES;
    [scrollView addSubview: handImageView];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(highlightImage)];
    [scrollView addGestureRecognizer:tapGesture];
    NSLog(@"tapGesture added to scrollView");


    [[cell contentView] addSubview:scrollView];

    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
    [pageControl setNumberOfPages:4];
    [[cell contentView] addSubview:pageControl];

My tapGesture is registering with the selector, hightlightImage, and just logging that its calling this method correctly.

- (void) highlightImage
{
    NSLog(@"tapping tapping");
}

What I REALLY am working for is the ability to highlight/select these images if tapped, and highlight/deselect them if tapped again. I will have another button on screen that will navigate to the next page (irrelevant here).

Am I going down the right path here? Obviously I will populate an NSArray of UIImages and populate the scrollView that way, but just spiking it out for now. If someone could give me some direction or an example of how to make each button separately selectable/de-selectable, that would be GREAT:)

Upvotes: 0

Views: 1784

Answers (1)

amattn
amattn

Reputation: 10065

Use UIButton instead of UIImageView.

It's got built-in selected functionality.

get rid of your gestures and add highlightImage as the action to every button.

make your highlightImage into highlightImage:(id)sender

sender should be a button so you can just do

[sender setSelected:YES];

Upvotes: 2

Related Questions