在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/C/ iCarousel能不能快速滾動(dòng),用于一組照片滾動(dòng)抽獎(jiǎng)這樣。我發(fā)現(xiàn)滾動(dòng)速度慢的很

iCarousel能不能快速滾動(dòng),用于一組照片滾動(dòng)抽獎(jiǎng)這樣。我發(fā)現(xiàn)滾動(dòng)速度慢的很。

//初始化
- (iCarousel *)icarouselView {
    
    if (!_icarouselView) {
        _icarouselView = [[iCarousel alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 200)];
        _icarouselView.pagingEnabled = YES;
        _icarouselView.scrollSpeed = 100.0;
        _icarouselView.delegate = self;
        _icarouselView.dataSource = self;
        [self.view addSubview:_icarouselView];
        
    }
    return _icarouselView;
}


//開啟滾動(dòng)(抽獎(jiǎng))
    __weak __typeof(&*self)weakSelf = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
            [weakSelf.icarouselView scrollToItemAtIndex:self.icarouselView.currentItemIndex+1 animated:YES];
    }];
//暫停(關(guān)閉定時(shí)器)
    [self.timer invalidate];
    self.timer = nil;
回答
編輯回答
過客

iCarousel切換的慢是不是速度問題, 而是和在一定時(shí)間內(nèi)滾動(dòng)的Offset有關(guān).
每一個(gè)item切換的Offset是通過時(shí)間計(jì)算的, 然后iCarousel再調(diào)用[self scrollByOffset:[self minScrollDistanceFromOffset:_scrollOffset toOffset:offset] duration:duration];滾動(dòng)視圖.

  • iCarousel源碼
NSTimeInterval time = MIN(1.0, (currentTime - _startTime) / _scrollDuration);
delta = [self easeInOut:time];
_scrollOffset = _startOffset + (_endOffset - _startOffset) * delta;
[self didScroll];

需要改變的是scrollOffset, scrollOffset和時(shí)間, 也就是time計(jì)算出delta = [self easeInOut:time];, 所有我建了一個(gè)分類, 其中重寫了easeInOut方法, 從而調(diào)整scrollOffset

#import <iCarousel/iCarousel.h>

@interface iCarousel (speed)

/** 滾動(dòng)速度 */
@property (nonatomic, assign) CGFloat speed;

@end
#import "iCarousel+speed.h"
#import <objc/runtime.h>

@implementation iCarousel (speed)

static NSString * const iCarousel_speed = @"iCarousel_speed";

- (CGFloat)easeInOut:(CGFloat)time
{
        return self.speed;
//    return 0.32;
//    return (time < 0.5)? 0.5 * pow(time * 2.0, 3.0): 0.5 * pow(time * 2.0 - 2.0, 3.0) + 1.0;
}

- (void)setSpeed:(CGFloat)speed
{
    objc_setAssociatedObject(self, &iCarousel_speed, @(speed), OBJC_ASSOCIATION_RETAIN);
    
}

- (CGFloat)speed
{
    return [objc_getAssociatedObject(self, &iCarousel_speed) doubleValue];
}

@end

speed越小速度越快, 設(shè)置_icarouselView.speed = 0.32;


自測: 滾動(dòng)時(shí)略有卡頓, 還不夠完美, 但是滾動(dòng)速度上確實(shí)提高了不少.

2017年5月23日 03:21
編輯回答
別硬撐

找到一個(gè)替代的方式。原本想著是連貫的滾動(dòng),頁尾接著頁首的,下面這個(gè)設(shè)置duration=0 可以一張張快速的展示,也能滿足需求了

[weakSelf.icarouselView scrollToItemAtIndex:self.icarouselView.currentItemIndex+1 duration:0];
2018年9月11日 02:26