Dev. App Start

Discussion in 'iOS Programming' started by nactusberrilli, Aug 31, 2012.

  1. macrumors newbie

    Joined:
    Aug 31, 2012
    #1
    Hi there

    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:

    Code:
    - (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:@"TimeCell%d.png",i]]highlightedImage:[UIImage imageNamed:[NSString stringWithFormat:@"TimeCell%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

    Code:
    #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 addImagesToView are on top of the scroll. That's my desired hierarchy.

    Thank you very much.
     
  2. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Why have you written your own scroller? What is wrong with the built in UIScrollView?
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    A suggestion: You should be using isKindOfClass: or isMemberOfClass: to check against class type.
     
  4. thread starter macrumors newbie

    Joined:
    Aug 31, 2012
    #4
    Dejo: What would be a better way to perform that compression?

    ArtOfWarefare: My initial idea was to give myself some room for future modifications. I was planning to add some delays and some caps to it.

    Thanks guys.
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Use one of the methods I suggested.
     
  6. thread starter macrumors newbie

    Joined:
    Aug 31, 2012
    #6
    Alright, thanks.
     

Share This Page