Kyuubi
Kyuubi

Reputation: 1240

Move a UIView based on the content offset for UIScrollView

I am trying to pull down a UIView (Like a Pull Down Menu) when the user scrolls below a Y-offset of 0.0 in the UITable View. If the user pulls down below -80.0 Y-Offset, then the PullDownMenu locks itself , until the User scrolls the other way.

My implementation for just the UITableView's ScrollView is as follows : [lock:false initially]

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if(isChangingOffset)
        return;
    if(resetDrag)
    {
        [self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
        resetDrag = false;
    }
    float xx = scrollView.contentOffset.y;
NSLog(@"Offset :%f",xx);
    if(xx - begginOffset > 0.0 && lock && !doneDragging)
    {
        offsetChange = xx - begginOffset;
        begginOffset = xx;
        lock = false;
        [self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
    }
    if(lock){
        [self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
    }
    if(xx <=-80.0)
    {
        [self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
        lock = true;
    }
}

- (void)setScrollViewOffset:(UIScrollView *)scrollView offsetTo:(CGPoint)offset{

- (void)setScrollViewOffset:(UIScrollView *)scrollView offsetTo:(CGPoint)offset{
    isChangingOffset = true;
    scrollView.contentOffset = CGPointMake(0, -80.0);
    isChangingOffset = false;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    float x = scrollView.contentOffset.y;
    begginOffset = x;
    doneDragging = false;
    if(lock){
        resetDrag = true;
    }
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    doneDragging = true;
}

Here is a working video of how it looks : Video

The blue color is a UIView that I have added as a subview in the UITableView.

My problem is , I am trying to pull down a UIView,lets call it menuView (that is not a subview of the tableView) based on the UITableView's contentOffset. I could have simply added the menuView in the UITableView like I added the blue color view. But that is only accessible via the table, that is when I scroll to the top and drag it down. But I want the menuView to be 'pull-able' like the notification center at any time. On using the Y-contentOffset of scroll view, the menuview pull down animation is not smooth. And it stops midway or goes too low. It is jerky and not always the same. How can I implement this ?

Upvotes: 1

Views: 5455

Answers (1)

BlackSheep
BlackSheep

Reputation: 1087

Here the sample code for your UIScrollView :

ViewController.h

@interface ViewController: UIViewController {

    UIScrollView *scrollView;
}

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;

ViewController.m

@implementation ViewController

@synthesize scrollView;

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self scrollView];
    }

    - (void)scrollText{

        [scrollView setContentSize:CGSizeMake(320, 800)];
        scrollView.scrollEnabled = YES;
        scrollView.pagingEnabled = YES;
        scrollView.clipsToBounds = YES;

    }

And iside you can put wat you want, from code or interface builder.

For a PullDownMenu you can see this GitHub:

MBPullDownController

Hope this help you and simplify your code ;)

Upvotes: 2

Related Questions