您好,登錄后才能下訂單哦!
PagingEnabled只能翻過整頁,下面幾個簡單的設置即可實現
技術點:
1. 創建一個繼承UIView的視圖,并設置clipsToBounds= YES
2. 添加一個UIscrollView控件,將其寬度設置為自定義翻頁的寬度
3. 設置UIScrollview 的clipsToBounds= NO
4. 確保本View的寬度大于UIScrollView的寬度用于顯示預覽內容
5. 重寫本View的hittest方法,為了確保用戶滑動UIscrollview以外的空間時也可以觸發UIscrollview滑動
ok! 下面是代碼,為了方便,使用圖片作為顯示的每一頁
#define kLJItemWidth 240
@implementation MyScrollview {
UIScrollView *scrollview;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
scrollview = ({
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 0, kLJItemWidth, frame.size.height)];
scroll.pagingEnabled = YES;
scroll.clipsToBounds = NO;
scroll;
}) ;
[self addSubview:scrollview];
self.clipsToBounds = YES;
}
return self;
}
-(void)loadImages:(NSArray *)array{
int index = 0;
[scrollview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
for(NSString * name in array){
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage p_w_picpathNamed:name]];
iv.contentMode = UIViewContentModeScaleToFill;
CGRect fra = iv.frame;
fra.size.width = kLJItemWidth;
fra.origin.x = index * kLJItemWidth;
iv.frame = fra;
[scrollview addSubview:iv];
index++;
}
scrollview.contentSize = CGSizeMake(scrollview.frame.size.width*index, scrollview.frame.size.height);
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
if ([view isEqual:self])
{
for (UIView *subview in scrollview.subviews)
{
CGPoint offset = CGPointMake(point.x - scrollview.frame.origin.x + scrollview.contentOffset.x - subview.frame.origin.x,
point.y - scrollview.frame.origin.y + scrollview.contentOffset.y - subview.frame.origin.y);
if ((view = [subview hitTest:offset withEvent:event]))
{
return view;
}
}
return scrollview;
}
return view;
}
@end
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。