溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在iOS中實現(xiàn)一個圖片自動切換效果

發(fā)布時間:2021-04-16 17:35:47 來源:億速云 閱讀:218 作者:Leah 欄目:移動開發(fā)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在iOS中實現(xiàn)一個圖片自動切換效果,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

具體內(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支持拖動分頁
 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];
 }
}
 
// 正滾動時執(zhí)行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
 CGFloat offX = self.imageScrollView.contentOffset.x;//(0,0)距離content內(nèi)部左上頂點的x軸長度
 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];//釋放定時器
 self.timer = nil;
}
 
// 開始準(zhǔn)備滾動時執(zhí)行 移除定時滾動操作
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
 NSLog(@"~~~scrollViewWillBeginDragging");
 [self removeScrollTimer];
}
 
// 結(jié)束滾動后執(zhí)行 添加定時滾動操作
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
 NSLog(@"~~~scrollViewDidEndDragging");
 [self addScrollTimer];
}
@end

上述就是小編為大家分享的怎么在iOS中實現(xiàn)一個圖片自動切換效果了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI