這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在iOS中實(shí)現(xiàn)一個(gè)圖片自動(dòng)切換效果,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開(kāi)發(fā)、微網(wǎng)站、小程序定制開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都做網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷(xiāo)經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷(xiāo)、管理等多方位專(zhuān)業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類(lèi)型的建設(shè)項(xiàng)目的能力。
具體內(nèi)容如下
#import "ViewController.h" #define ImageViewCount 5 @interface ViewController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView; @property (weak, nonatomic) IBOutlet UIPageControl *imageViewPageControl; @property (strong, nonatomic) NSTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self addImageView2ScrollView]; self.imageScrollView.contentSize = CGSizeMake(self.imageScrollView.frame.size.width * ImageViewCount, 0); self.imageScrollView.delegate = self; self.imageScrollView.pagingEnabled = YES;//UIScrollView支持拖動(dòng)分頁(yè) self.imageViewPageControl.numberOfPages = ImageViewCount; [self addScrollTimer]; } - (void)rotatePic{ int currentPageIndex = self.imageViewPageControl.currentPage; if(++currentPageIndex == 5){ currentPageIndex = 0; } CGFloat offsetX = currentPageIndex * self.imageScrollView.frame.size.width; [UIView animateWithDuration:1 animations:^{ self.imageScrollView.contentOffset = CGPointMake(offsetX, 0); }]; } /**添加圖片到imageScrollView*/ - (void)addImageView2ScrollView{ CGFloat imageWidth = self.imageScrollView.frame.size.width; CGFloat imageHeight = self.imageScrollView.frame.size.height; for(int i = 0;i <= ImageViewCount;i++){ UIImageView *imageInScroll = [[UIImageView alloc] init]; imageInScroll.frame = CGRectMake(i * imageWidth, 0, imageWidth, imageHeight); imageInScroll.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d",i + 1]]; [self.imageScrollView addSubview:imageInScroll]; } } // 正滾動(dòng)時(shí)執(zhí)行 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat offX = self.imageScrollView.contentOffset.x;//(0,0)距離content內(nèi)部左上頂點(diǎn)的x軸長(zhǎng)度 NSLog(@"~~~~~~~%f ^^^^^^%f", offX, self.imageScrollView.frame.size.width); int currentPageIndex = (offX + .5f * self.imageScrollView.frame.size.width) / self.imageScrollView.frame.size.width; self.imageViewPageControl.currentPage = currentPageIndex; } - (void)addScrollTimer{ self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(rotatePic) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)removeScrollTimer{ [self.timer invalidate];//釋放定時(shí)器 self.timer = nil; } // 開(kāi)始準(zhǔn)備滾動(dòng)時(shí)執(zhí)行 移除定時(shí)滾動(dòng)操作 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ NSLog(@"~~~scrollViewWillBeginDragging"); [self removeScrollTimer]; } // 結(jié)束滾動(dòng)后執(zhí)行 添加定時(shí)滾動(dòng)操作 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ NSLog(@"~~~scrollViewDidEndDragging"); [self addScrollTimer]; } @end
上述就是小編為大家分享的怎么在iOS中實(shí)現(xiàn)一個(gè)圖片自動(dòng)切換效果了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱(chēng):怎么在iOS中實(shí)現(xiàn)一個(gè)圖片自動(dòng)切換效果
文章URL:http://www.chinadenli.net/article10/ieojdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、企業(yè)建站、動(dòng)態(tài)網(wǎng)站、微信公眾號(hào)、外貿(mào)建站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)