ios UICollectionView滑動時操做

點開UICollectionViewDelegate,發現有@protocol UICollectionViewDelegate <UIScrollViewDelegate>。spa

因此只要實現UIScrollViewDelegate的code

- (void)scrollViewDidScroll:(UIScrollView *)scrollView; 方法,就能夠重寫UICollectionView滑動操做blog

例如,在UICollection上方有一個名爲testImg的ImageView,要跟着UICollection的滑動一塊兒動,那麼能夠使用下面方法io

 1 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
 2 {
 3     CGPoint point=scrollView.contentOffset;
 4     NSLog(@"%f,%f",point.x,point.y);
 5     
 6     CGRect frame = [_testImg frame];
 7     frame.origin.y = 43-point.y;
 8     _testImg.frame = frame;
 9     
10     frame = [scrollView frame];
11     frame.origin.y = 179-point.y;
12     scrollView.frame = frame;
13 }

須要注意的是,第7行的43和第11行的179分別爲testImg和UICollectionView初始的y軸值,不是滑動以前的值。若是使用class

控件.origin.y -= point.y;test

y值就會快速變小,控件瞬間飛出屏幕scroll

上述代碼中,point是滑動以後的偏移量,手指上滑,偏移y爲正方法

相關文章
相關標籤/搜索