Hide UIView using alpha when user starts scrolls o

2019-05-18 20:39发布

viewController is composed of 2 main views. A container view and a table view.

The container view is the top section with all the labels such as the day and score.

I want this container view to hide when a user starts scrolling through the days of the table view.

So far I have:

  1. Hooked up an IBOutlet for this container view so I could reference it in the code.
  2. Conformed to the UIScrollViewDelegate
  3. Implimented all required methods for the UITableView

I know UITableView is a subclass of UIScrollView. Without a "proper" UIScrollView I am confused how to go about using the scrollViewDidScroll method and changing the container view alpha to 0 when I am scrolling in the tableView.

I also want the alpha to return to 1 when the user stops scrolling.

thanks.

Note I am not using a UITableViewController

screenshot

4条回答
手持菜刀,她持情操
2楼-- · 2019-05-18 20:57

You should use a UITableViewController instead of a UIViewController and then drag a UIView in the header section of the UITable. This will cause the UIView to get out of the way on scrolling right out of the box with no coding at all.

查看更多
▲ chillily
3楼-- · 2019-05-18 21:03

Use the following methods directly in controller. They will work :

// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
查看更多
叼着烟拽天下
4楼-- · 2019-05-18 21:09

If you already set your UITableViewDelegate IBOutlet your controller you will receive all UITableViewDelegate methods from your UITableView as well as all UIScrollViewDelegate methods, should you implement them.

If you would like to hide a particular view when the user starts scrolling I'd use the -scrollViewWillBeginDragging method

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
{
    [UIView animateWithDuration:0.5f animations:^{
        someView.alpha = 0.0f;
    }];
}

If you would like to show it again after the user stops scrolling, just use the method -scrollViewDidEndDragging:willDecelerate:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
    [UIView animateWithDuration:0.5f animations:^{
        someView.alpha = 1.0f;
    }];
}
查看更多
Deceive 欺骗
5楼-- · 2019-05-18 21:09

What have you made UIScrollViewDelegate?

I just took a UITableViewController in my current project and added UIScrollViewDelegate

 @interface YourTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>

then stuck a comment in

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

and it called once as required (scrollViewDidScroll: will fire repeatedly). So I presume calling one of the UIView animateWithDuration: methods will complete the task you require?

查看更多
登录 后发表回答