UIScrollView设置了contentSize后还是没办法滚动?

1.最常见的原因是 
contentSize 这个属性,比uiscrollview的frame要小, 无需滚动, 自然就滚动不了。 
scrollenabled 这个属性,标识着是否允许滚动,要言设成yes 

2.但是今天我遇到了另外一种情况

也就是将ScrollView放在一个UIView中, 该设置的属性都设置了, 结果还是没办法滚动..

最后发现由于没有设置UIView的frame, 但由于scrollView还是能显示出来, 所以很少想到这个问题, 所以没办法滚动

设置之后UIView的frame之后, 一切就正常了...

转载于:https://www.cnblogs.com/Rinpe/p/5061746.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
定时滚动和循环滚动,可点击图片和PageController #import "ASIFormDataRequest.h" #import "GWPublicClass.h" @interface ViewController ()<UIScrollViewDelegate> @end @implementation ViewController { UIScrollView * headScrollView; UIPageControl * pageControl; NSArray * colorArray; NSTimer * myTimer; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } #pragma UIScrollView delegate -(void)scrollToNextPage:(id)sender { int pageNum = pageControl.currentPage; CGSize viewSize = headScrollView.frame.size; CGRect rect = CGRectMake((pageNum+2)*viewSize.width, 0, viewSize.width, viewSize.height); [headScrollView scrollRectToVisible:rect animated:NO]; pageNum++; if (pageNum == colorArray.count) { CGRect newRect=CGRectMake(viewSize.width, 0, viewSize.width, viewSize.height); [headScrollView scrollRectToVisible:newRect animated:NO]; } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = headScrollView.frame.size.width; int currentPage = floor((headScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1; if (currentPage == 0) { pageControl.currentPage = colorArray.count-1; }else if(currentPage == colorArray.count+1){ pageControl.currentPage=0; } pageControl.currentPage = currentPage-1; } -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [myTimer invalidate]; } -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES]; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth = headScrollView.frame.size.width; CGFloat pageHeigth = headScrollView.frame.size.height; int currentPage=floor((headScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1; NSLog(@"the current offset==%f",headScrollView.contentOffset.x); NSLog(@"the current page==%d",currentPage); if (currentPage == 0) { [headScrollView scrollRectToVisible:CGRectMake(pageWidth*colorArray.count, 0, pageWidth, pageHeigth) animated:NO]; pageControl.currentPage = colorArray.count-1; NSLog(@"pageControl currentPage==%d",pageControl.currentPage); NSLog(@"the last image"); return; }else if(currentPage == [colorArray count]+1){ [headScrollView scrollRectToVisible:CGRectMake(pageWidth, 0, pageWidth, pageHeigth) animated:NO]; pageControl.currentPage=0; NSLog(@"pageControl currentPage==%d",pageControl.currentPage); NSLog(@"the first image"); return; } pageControl.currentPage=currentPage-1; NSLog(@"pageControl currentPage==%d",pageControl.currentPage); } - (void)pageTurn:(UIPageControl *)sender { int pageNum = pageControl.currentPage; CGSize viewSize = headScrollView.frame.size; [headScrollView setContentOffset:CGPointMake((pageNum+1)*viewSize.width, 0)]; NSLog(@"myscrollView.contentOffSet.x==%f",headScrollView.contentOffset.x); NSLog(@"pageControl currentPage==%d",pageControl.currentPage); [myTimer invalidate]; } - (void)handleTapGesture:(UITapGestureRecognizer*)gesture { NSLog(@"UITapGesture被调用了%d",gesture.view.tag); // ... } - (void)viewDidLoad { [super viewDidLoad]; colorArray = @[[UIColor redColor],[UIColor greenColor],[UIColor blueColor]]; headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 320, 200)]; headScrollView.backgroundColor = [UIColor blackColor]; [self.view addSubview:headScrollView]; CGFloat Width= 320; CGFloat Height= 200; UIImageView * firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)]; firstImageView.userInteractionEnabled = YES; firstImageView.tag = 50+colorArray.count-1; firstImageView.backgroundColor = colorArray[colorArray.count-1]; [headScrollView addSubview:firstImageView]; UITapGestureRecognizer * gestd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [firstImageView addGestureRecognizer:gestd]; for (int i=0; i<colorArray.count; i++) { UIImageView * subImageView=[[UIImageView alloc] initWithFrame:CGRectMake(Width*(i+1), 0, Width, Height)]; subImageView.backgroundColor = colorArray[i]; subImageView.userInteractionEnabled = YES; subImageView.tag = 50+i; subImageView.frame=CGRectMake(Width*(i+1), 0, Width, Height); [headScrollView addSubview: subImageView]; UITapGestureRecognizer * gestd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [subImageView addGestureRecognizer:gestd]; } UIImageView * lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Width*(colorArray.count+1), 0, Width, Height)]; lastImageView.userInteractionEnabled = YES; lastImageView.backgroundColor = colorArray[0]; lastImageView.tag = 50; [headScrollView addSubview:lastImageView]; headScrollView.contentSize = CGSizeMake(Width*(colorArray.count+2), Height); headScrollView.pagingEnabled = YES; headScrollView.delegate = self; [headScrollView scrollRectToVisible:CGRectMake(Width, 0, Width, Height) animated:YES]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 290, 120, 20)]; pageControl.numberOfPages = colorArray.count; pageControl.backgroundColor = [UIColor greenColor]; pageControl.enabled = YES; pageControl.currentPage = 0; [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:pageControl]; myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES]; } - (void)enterDetail { NSLog(@"tap is "); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值