Search code examples
ioscocoa-touchios5uiscrollviewtouchesbegan

Buttons get stuck on highlighted


I have just started working on a project and I already have a few problems and errors. I was wondering if you guys could help me figure out where the bug is.

Quick overview: I have two files: (ViewController.m and Scroller.m). In the ViewController I have some code under ViewDidLoad (which I'll show in a second), I also have a function (addImagesToView) that does what it says, and some touchesBegan,moved and ended for intractability.

In the Scroller I decided to rewrite some of the "-(void)touches" functions implementations.

The problem I have: is that the buttons (coming from the addImagesToView function) get stuck on highlighted. I performed some tests with an NSLog to see which "touches" working and which don't. "touchesMoved" doesn't work properly. If the user drags downward, the log stops after about 3 or 4 lines of text. I think that it interferes with the scroll somehow.

This is the content of ViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bgPNG.png"]];

    imageView.frame = CGRectMake(0, 0, 320, 480);
    [self.view addSubview:imageView];

    //UIColor *background = [[UIColor alloc]initWithPatternImage:imageView.image];
    //self.view.backgroundColor = background;

    CGRect fullScreen = [[UIScreen mainScreen] applicationFrame];

    scroll = [[Scroller alloc] initWithFrame:fullScreen];
    scroll.contentSize = CGSizeMake(320, 2730);

    scroll.delaysContentTouches = NO;
    //scroll.canCancelContentTouches = NO;
    scroll.scrollEnabled  = YES;

    [self.view addSubview:scroll];

    buttons = [[NSMutableArray alloc]init];

    [self addImagesToView];



}

-(void) addImagesToView{




    CGFloat yCoordinate = 35;

    for (int i=1; i<=18; i++) {

        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"picture%d.png",i]]highlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"picture%dHG.png",i]]];

        CGRect position = CGRectMake(105, yCoordinate, IMAGE_SIZE, IMAGE_SIZE);
        image.frame = position;

        [scroll addSubview:image];

        image.userInteractionEnabled = YES;
        image.tag = i;

        [buttons addObject:image];
        yCoordinate += 150;


    }
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Get any touch
    UITouch *t = [touches anyObject];


    if ([t.view class] == [UIImageView class])
    {

        // Get the tappedImageView
        UIImageView *tappedImageView = (UIImageView*) t.view;
        tappedImageView.highlighted = YES;
    }
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [touches anyObject];
    if ([t.view class] == [UIImageView class])
    {

        // Get the tappedImageView
        UIImageView *tappedImageView = (UIImageView*) t.view;
        tappedImageView.highlighted = NO;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources th at can be recreated.
}

@end

and this is the Scroller.m

#import "Scroller.h"

@implementation Scroller

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging)
        [self.nextResponder touchesBegan: touches withEvent:event];
    else
        [super touchesBegan: touches withEvent: event];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [touches anyObject];
    if ([t.view class] == [UIImageView class])
    {

        // Get the tappedImageView
        UIImageView *tappedImageView = (UIImageView*) t.view;
        tappedImageView.highlighted = NO;
    }

}


-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!self.dragging)
        [self.nextResponder touchesEnded: touches withEvent:event];
    else
        [super touchesEnded: touches withEvent: event];        // Get the tappedImageView

}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end

I have also tried to implement all the touches in the ViewController, but I don't know how to make it read from the scroll (please notice the diff. between SCROLL and SCROLLER).

As you can tell I've tried to order them like this: view -> backGroundImage ->scroller ->addImagesToView(function), so that the images that come out from [I]addImagesToView[/I] are on top of the scroll. That's my desired hierarchy.

Thank you very much.


Solution

  • You don't have to write your own touch handling routines for button presses. Instead of using UIImageViews, just create UIButtons instead and hook into their UIControlEventTouchUpInside event.