這篇文章給大家分享的是有關(guān)iOS如何實(shí)現(xiàn)帶有縮放效果的自動(dòng)輪播圖的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)公司專注于晉江企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,成都做商城網(wǎng)站。晉江網(wǎng)站建設(shè)公司,為晉江等地區(qū)提供建站服務(wù)。全流程按需定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)可直接設(shè)置frame然后加載到視圖上使用。
效果就是這樣的,圖片切換的過程中還是有卡頓,不夠流暢,后續(xù)更新。
直接上代碼。
.h文件包含:
#import@interface CustomScrollView : UIView @property (strong,nonatomic) NSArray *imageArr; @end
.m文件包含:
#import "CustomScrollView.h" @interface CustomScrollView ()@property (strong,nonatomic) UIScrollView *scrollView; @property (strong,nonatomic) UIImageView *backImageView; @property (strong,nonatomic) UIImageView *leftIamgeView; @property (strong,nonatomic) UIImageView *middleImageView; @property (strong,nonatomic) UIImageView *rightImageView; @property (strong,nonatomic) UIPageControl *pageControl; //高度 @property (assign,nonatomic) CGFloat scrollViewHeight; //手動(dòng)造成的偏移量 @property (assign,nonatomic) CGFloat offsetX; @property (strong,nonatomic) NSTimer *timer; //屬于計(jì)時(shí)器方法動(dòng)畫持續(xù)時(shí)間 ? @property (assign,nonatomic) BOOL timerAnimation; @end //左右內(nèi)容的大偏移量 #define OFFSET_MAX ([UIScreen mainScreen].bounds.size.width-64)*0.9 @implementation CustomScrollView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.scrollViewHeight = frame.size.height; [self addSubview:self.backImageView]; [self addSubview:self.scrollView]; [self addSubview:self.pageControl]; [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.with.mas_equalTo(self); }]; [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.with.mas_equalTo(self); }]; [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.mas_equalTo(self); make.height.mas_equalTo(@20); }]; self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode]; [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]]; } return self; } - (NSArray *)imageArr { if (!_imageArr) { _imageArr = @[@"timg-0",@"timg-1",@"timg-2"]; } return _imageArr; } - (UIImageView *)backImageView { if (!_backImageView) { _backImageView = [[UIImageView alloc] init]; _backImageView.image = [UIImage imageNamed:@"bg_home"]; } return _backImageView; } - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] init]; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.contentSize = CGSizeMake(kScreenWidth+2*OFFSET_MAX, self.scrollViewHeight); [_scrollView setContentOffset:CGPointMake(OFFSET_MAX, 0)]; _scrollView.bounces = NO; _scrollView.delegate = self; _leftIamgeView = [[UIImageView alloc] init]; _leftIamgeView.image = [UIImage imageNamed:self.imageArr[0]]; _leftIamgeView.layer.cornerRadius = 5; _leftIamgeView.layer.masksToBounds = YES; _middleImageView = [[UIImageView alloc] init]; _middleImageView.image = [UIImage imageNamed:self.imageArr[1]]; _middleImageView.layer.cornerRadius = 5; _middleImageView.layer.masksToBounds = YES; _rightImageView = [[UIImageView alloc] init]; _rightImageView.image = [UIImage imageNamed:self.imageArr[2]]; _rightImageView.layer.cornerRadius = 5; _rightImageView.layer.masksToBounds = YES; [_scrollView addSubview:_leftIamgeView]; [_scrollView addSubview:_middleImageView]; [_scrollView addSubview:_rightImageView]; [_leftIamgeView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake((kScreenWidth-64)*0.9, self.scrollViewHeight*0.9)); make.centerY.mas_equalTo(_scrollView); make.right.mas_equalTo(_middleImageView.mas_left).offset(-20); }]; [_middleImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(kScreenWidth-64, self.scrollViewHeight)); make.centerY.mas_equalTo(_scrollView); make.left.mas_equalTo(_scrollView).offset(OFFSET_MAX+64/2); }]; [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(_leftIamgeView); make.centerY.mas_equalTo(_scrollView); make.left.mas_equalTo(_middleImageView.mas_right).offset(20); }]; } return _scrollView; } - (UIPageControl *)pageControl { if (!_pageControl) { _pageControl = [[UIPageControl alloc] init]; _pageControl.numberOfPages = self.imageArr.count; _pageControl.enabled = NO; _pageControl.currentPage = 0; _pageControl.hidesForSinglePage = YES; _pageControl.pageIndicatorTintColor = [UIColor whiteColor]; _pageControl.currentPageIndicatorTintColor = MAINCOLOR; } return _pageControl; } #pragma mark ------------ UIScrollViewDelegate -------- - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (self.timerAnimation) { return; } CGFloat offScale = scrollView.contentOffset.x - OFFSET_MAX; if (offScale < 0) { //右滑 self.leftIamgeView.layer.transformScale = 1 + 1.0/9*(fabs(offScale)/OFFSET_MAX); self.middleImageView.layer.transformScale = 1 - 0.1*(fabs(offScale)/OFFSET_MAX); } else { //左滑 self.rightImageView.layer.transformScale = 1 + 1.0/9*(fabs(offScale)/OFFSET_MAX); self.middleImageView.layer.transformScale = 1 - 0.1*(fabs(offScale)/OFFSET_MAX); } } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { CGPoint offset = scrollView.contentOffset; self.offsetX = offset.x; if ((fabs(self.offsetX-OFFSET_MAX) >= OFFSET_MAX*0.5)) { if (!decelerate) { //替換圖片 [self exchangeImage]; } } else { //回彈復(fù)位 [scrollView setContentOffset:CGPointMake(OFFSET_MAX, 0) animated:YES]; self.leftIamgeView.layer.transform = CATransform3DIdentity; self.middleImageView.layer.transform = CATransform3DIdentity; self.rightImageView.layer.transform = CATransform3DIdentity; } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { //替換圖片 [self exchangeImage]; } #pragma mark ---------- exchangeImage ------ //替換圖片 - (void)exchangeImage { if (self.offsetX-OFFSET_MAX < 0) { //右滑 UIImage *rightImage = self.rightImageView.image; self.rightImageView.image = self.middleImageView.image; self.middleImageView.image = self.leftIamgeView.image; self.leftIamgeView.image = rightImage; if (self.pageControl.currentPage - 1 < 0) { self.pageControl.currentPage = self.pageControl.numberOfPages-1; } else { self.pageControl.currentPage -= 1; } } else { //左滑 UIImage *leftImage = self.leftIamgeView.image; self.leftIamgeView.image = self.middleImageView.image; self.middleImageView.image = self.rightImageView.image; self.rightImageView.image = leftImage; if (self.pageControl.currentPage + 1 >= self.pageControl.numberOfPages) { self.pageControl.currentPage = 0; } else { self.pageControl.currentPage += 1; } } [self.scrollView setContentOffset:CGPointMake(OFFSET_MAX, 0)]; self.leftIamgeView.layer.transform = CATransform3DIdentity; self.middleImageView.layer.transform = CATransform3DIdentity; self.rightImageView.layer.transform = CATransform3DIdentity; } #pragma mark ---------- timer -------- - (void)timerAction { self.offsetX = kScreenWidth+OFFSET_MAX-64; self.timerAnimation = YES; [UIView animateWithDuration:0.8 animations:^{ [self.scrollView setContentOffset:CGPointMake(self.offsetX, 0)]; self.rightImageView.layer.transformScale = 10/9.0; self.middleImageView.layer.transformScale = 0.9; } completion:^(BOOL finished) { self.timerAnimation = NO; [self exchangeImage]; self.rightImageView.layer.transform = CATransform3DIdentity; self.middleImageView.layer.transform = CATransform3DIdentity; }]; } @end
最后調(diào)用:
self.scrollView = [[CustomScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 170)]; [self.view addSubview:self.scrollView];
感謝各位的閱讀!關(guān)于“iOS如何實(shí)現(xiàn)帶有縮放效果的自動(dòng)輪播圖”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。