开发者

UIView inside UIScrollView issues

开发者 https://www.devze.com 2023-04-11 12:21 出处:网络
开发者_运维百科If I have a UIView as a subview of the UIScrollView and this scroll view is a horizontal scroll view, how do I know when the UIView (the subview) is out of the UIScrollView so that I ca
开发者_运维百科

If I have a UIView as a subview of the UIScrollView and this scroll view is a horizontal scroll view, how do I know when the UIView (the subview) is out of the UIScrollView so that I can remove it as a subview and store it somewhere else for reuse? Is there a delegate for this?


You can use the UIScrollViewDelegate method scrollViewDidEndDecelerating: and some custom code to achieve this.

- (void)viewDidLoad {
    [super viewDidLoad];
    //set theScrollView's delegate
    theScrollView.delegate = self;
}

//custom method for determining visible rect of scrollView
- (CGRect)visibleRectForScrollView:(UIScrollView *)scrollView; {
    CGFloat scale = (CGFloat) 1.0 / scrollView.zoomScale;
    CGRect visibleRect;
    visibleRect.origin = scrollView.contentOffset;
    visibleRect.size = scrollView.bounds.size;
    float theScale = 1.0 / scale;
    visibleRect.origin.x *= theScale;
    visibleRect.origin.y *= theScale;
    visibleRect.size.width *= theScale;
    visibleRect.size.height *= theScale;
    return visibleRect;
}

//UIScrollView Delegate method
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    BOOL viewVisisble = CGRectContainsRect([self visisbleRectForScrollView:theScrollView], theView.frame);
    if(!viewVisisble) {
        //do something
    }
}


Yes there is a delegate for this, you need to use the UIScrollViewDelegate.

http://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html

The method scrollViewDidScroll tell you when a scroll append, so in this function you could test the contentOffset property (scrollview.contentOffset.x) and then compare it with your view position and size (myView.frame.origin.x + myView.frame.size.width).

So basicaly you should do

if(scrollview.contentOffset.x > (myView.frame.origin.x + myView.frame.size.width))
 //Remove my view to reuse it

If you only have 2 views to display and just want to re-use each view you could find the view currently displayed like this:

    //Assuming your views had the same width and it is store in the pageWidth variable
    float currPosition = photoGalleryScrollView.contentOffset.x;
    //We look for the selected page by comparing his width with the current scroll view position
    int selectedPage = roundf(currPosition / pageWidth);
    float truePosition = selectedPage * pageWidth;
    int zone = selectedPage % 2;
    BOOL view1Active = zone == 0;
    UIView *nextView = view1Active ? view2 : view1;
    UIView *currentView = view1Active ? view1 : view2;

    //We search after the next page
    int nextpage = truePosition > currPos + 1 ? selectedPage-1 : selectedPage+1;

    //then we compare our next page with the selectd page
    if(nextpage > selectedPage){
         //show next view
    }
    else{
         //show previous view
    }

After that you need to add some content to nextView, add it to the scroll view and remove currentView when its hidden.

0

精彩评论

暂无评论...
验证码 换一张
取 消