iOS 그림 자동 전환 효과 구현
#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];
 }
}
 
//       
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
 CGFloat offX = self.imageScrollView.contentOffset.x;//(0,0)  content       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;
}
 
//                   
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
 NSLog(@"~~~scrollViewWillBeginDragging");
 [self removeScrollTimer];
}
 
//                 
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
 NSLog(@"~~~scrollViewDidEndDragging");
 [self addScrollTimer];
}
@end1.contentSize 속성 설정 에 주의 하 세 요.그 중에서 contentSize 는 스크롤 내용 의 크기 를 나타 낸다.
2.프 록 시 UIScrollViewDelegate 를 설정 해 야 사용 할 수 있 는 방법
타이머 NSTimer 활용 에 주의 하 시기 바 랍 니 다.
1.스 레 드 의 loop 에 타이머 추가
2.NSTimer 회수 완료 사용 에 주의
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
View의 레이아웃 방법을 AutoLayout에서 따뜻한 손 계산으로 하면 성능이 9.26배로 된 이야기이 기사는 의 15 일째 기사입니다. 어제는 에서 이었습니다. 손 계산을 권하는 의도는 없고, 특수한 상황하에서 계측한 내용입니다 화면 높이의 10 배 정도의 contentView가있는 UIScrollView 레이아...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.