Smooth Scrolling (not in tableview)

Discussion in 'iOS Programming' started by jnoxx, Oct 20, 2011.

  1. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #1
    Hey everyone,

    I have been bugging myself crazy over this. But since I determined to know what's going on, I want to figure it out.
    Let's say, I have a full view on top of my stack, and I want to slide it away with my finger. So let's say you have an iPad in Landscape which has in View ways an CGRect of his frame from 0,0, 1024, 748.
    When you put down your finger, and you want to slide it off screen, but while you are slowly sliding it should follow your finger, I have tried several solutions like this

    Code:
    #pragma mark - Touches
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
        UITouch *touch = [touches anyObject]; 
        startTouchPosition = [touch locationInView:self.view]; 
    } 
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
        UITouch *touch = touches.anyObject; 
        CGPoint location = [touch locationInView:self.view];
        if (location.x > startTouchPosition.x) {
                self.view.frame = CGRectMake(location.x, 0, self.view.frame.size.width, self.view.frame.size.height);
            }
        } else {
            CGFloat sideFloat = startTouchPosition.x - location.x;
            NSLog(@"location.x>> %f", sideFloat);
            self.view.frame = CGRectMake(self.view.frame.origin.y-sideFloat, 0, self.view.frame.size.width, self.view.frame.size.height);
        }
    }
    
    - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        if (fabsf(startTouchPosition.x - currentTouchPosition.x >= HORIZ_SWIPE_DRAG_MIN)) {
            [self scrollAway];
        }
        started = NO;
    }
    
    - (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        started = NO;
    }
    
    What I basicly do is set a start position, and try to make some simple equations (don't know perfect english word, not my mother language).
    So when you scroll left, it follows your finger smooth, not lagging like a pain in the ass because it's setting the frame every single pixel. and the other way around it's not working. Allthough my math works if I did a total of 250 pixels sliding (it's what the HORIZ_SWIPE_DRAG_MIN defines). Then it will scroll away by force, when released.
    So, not sure if I made it clear.
    I have a full screen image or a view, and I want to slide it off screen, but it has to follow my finger smoothly. Not a bit slashery jabbing around my finger.

    A perfect example is an app I have (which is free for the moment), called 500px. (no promotion intended with this), pure "wanting to learn" promotion here :)

    Hope anyone has come across this, and could help me out! :)

    Greetings, Noxx
     
  2. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    It sounds like the behavior you want could pretty easily be gotten by using a UIScrollView with paging enabled, but maybe I don't understand exactly what you're looking for...
     
  3. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #3
    Go with UIGestureRecognizers, unless you really have to build an application that will run before the 3.2 OS. Is there some reason you absolutely have to handle the touches directly?
     
  4. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Those touches methods are all antiques. Use a pan gesture recognizer.
     
  5. jnoxx thread starter macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    Oh man, I haven't thought of that, i'll take a look at it later guys.
    Appreciate it, and will post code back if I find it (but have some other stuff to do later)
     

Share This Page