Iphone UIView内部UIScrollView问题
如果我将UIView作为UIScrollView的子视图,而此滚动视图是水平滚动视图,那么我如何知道UIView的子视图何时不在UIScrollView中,以便将其作为子视图删除并存储在其他位置以供重用?是否有此委托?是的,有此委托,您需要使用UIScrollViewDelegate scrollViewDidScroll方法会在滚动追加时告诉您,因此在此函数中,您可以测试contentOffset属性scrollview.contentOffset.x,然后将其与视图位置和大小myView.frame.origin.x+myView.frame.size.width进行比较 所以基本上你应该这样做Iphone UIView内部UIScrollView问题,iphone,objective-c,ipad,uiscrollview,Iphone,Objective C,Ipad,Uiscrollview,如果我将UIView作为UIScrollView的子视图,而此滚动视图是水平滚动视图,那么我如何知道UIView的子视图何时不在UIScrollView中,以便将其作为子视图删除并存储在其他位置以供重用?是否有此委托?是的,有此委托,您需要使用UIScrollViewDelegate scrollViewDidScroll方法会在滚动追加时告诉您,因此在此函数中,您可以测试contentOffset属性scrollview.contentOffset.x,然后将其与视图位置和大小myView.f
if(scrollview.contentOffset.x > (myView.frame.origin.x + myView.frame.size.width))
//Remove my view to reuse it
如果只有2个视图要显示,并且只想重新使用每个视图,则可以找到当前显示的视图,如下所示:
//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
}
之后,您需要向nextView添加一些内容,将其添加到滚动视图,并在其隐藏时删除currentView。您可以使用UIScrollViewDelegate方法ScrollViewDiEndDeclaring:和一些自定义代码来实现这一点
- (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
}
}
总体用例是什么?通常,当他们向后滚动时,您会将UIView保留在那里。用例是我想要一个UIView池,因为屏幕上一次只有3个子视图。。当它们向后滚动时,我希望重用池中的UIView。有点像UITableView单元格,其中可以重用的Rect是CGRect,但返回的是CGFloat?显然这是一个错误,你可以在方法的代码中看到它说CGRect,如果我想知道这个视图是因为用户水平向左还是水平向右滑动而消失了呢?