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 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制