Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jnoxx

macrumors 65816
Original poster
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
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
 
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...
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.