freebugone 2015-08-11 09:03 采纳率: 0%
浏览 1505
已结题

UISrcollView自动滚动

//
//

//
//手动左右无缝已经写完,差自动滚动,<方向:向右>带切换动画.
//

//

//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic ,strong)UIScrollView *scrollView;
@property (nonatomic ,strong)UIPageControl *pageControl;
@property (nonatomic ,strong)NSTimer *timer;
@property (nonatomic ,assign)CGFloat width;
@property (nonatomic ,assign)CGFloat height;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    _width = [[UIScreen mainScreen] bounds].size.width;
    _height = [[UIScreen mainScreen] bounds].size.height;
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = self.view.bounds;
    _scrollView.backgroundColor = [UIColor brownColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(_width*7, _height);
    _scrollView.contentOffset = CGPointMake(_width, 0);

    for (int i = 0; i<7; i++)
    {
    UIImageView imgV = [UIImageView new];
    imgV.frame = CGRectMake(i
    _width, 0, width, _height);
    imgV.contentMode = UIViewContentModeScaleAspectFit;
    NSString *str = [NSString stringWithFormat:@"IMG
    %i",i+1];
    imgV.image = [UIImage imageNamed:str];
    [_scrollView addSubview:imgV];
    }

    [self.view addSubview:_scrollView];

    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, _height*0.8, _width-40, 50)];
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    _pageControl.numberOfPages = 5;
    _pageControl.currentPage = 0;
    [self.view addSubview:_pageControl];

// _timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(dddselecteddd) userInfo:nil repeats:YES];

}

//- (void)dddselecteddd
//{
//

// CGFloat aa = _scrollView.contentOffset.x;
// aa += _width;
// [UIView animateWithDuration:0.5 animations:^{
// _scrollView.contentOffset= CGPointMake(aa, 0);
// }];
//

//

// if (_scrollView.contentOffset.x > 5*_width)
// {
// _scrollView.contentOffset = CGPointMake(0, 0);
// }
//

//

//}

#pragma - mark 滑动;

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    NSInteger page = _scrollView.contentOffset.x/_width;
    _pageControl.currentPage = page-1;

    if (_scrollView.contentOffset.x == 6*_width)
    {
    _scrollView.contentOffset = CGPointMake(_width, 0);
    }
    else if (_scrollView.contentOffset.x == 0*_width)
    {
    _scrollView.contentOffset = CGPointMake(5*_width, 0);
    }
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

  • 写回答

1条回答 默认 最新

  • DaveLeeCN 2015-08-11 09:46
    关注

    原理就是一个长条形的div放置几张图片,然后复制这个div放到右边(上下左都可以,看你滚动方向),隐藏滚动条,然后设置滚动条的偏移,滚动条滚动到最右侧的时候偏移赋值为0,从头开始(我是做js的,你写的真心看不懂,仅供参考)

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧