Objective-C 무한 순환 재생 기 구현
9343 단어 Objective-C윤파 기
구현 코드:
1.컨트롤 러
//
// AppDelegate.m
//
//
// Created by zhangmi on 16/5/16.
// Copyright © 2016 Paramount Pictures. All rights reserved.
//
#import "ViewController.h"
#import "SNInfiniteScrollView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray * images = [NSMutableArray array];
for (int i = 0; i < 5; i++) {
NSString * imageName = [NSString stringWithFormat:@"ad_%02d", i]; //img01
UIImage * image = [UIImage imageNamed:imageName];
[images addObject:image];
}
UIView * scrollView = [SNInfiniteScrollView scrollViewWithFrame:CGRectMake(0, 20, 414, 200) superView:self.view images:images scrollDirection:ScrollDirectionHorizontal pageIndicatorTintColor:[UIColor lightGrayColor] currentPageIndicatorTintColor:[UIColor orangeColor] imageViewcontentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:scrollView];
}
@end
2.콘 텐 츠 인터페이스 설정 보이 기
//
// AppDelegate.m
//
//
// Created by zhangmi on 16/5/16.
// Copyright © 2016 Paramount Pictures. All rights reserved.
//
#import "SNInfiniteScrollView.h"
static int const ImageViewCount = 3;
#define scrollViewWidth self.scrollView.frame.size.width
#define scrollViewHeight self.scrollView.frame.size.height
@interface SNInfiniteScrollView () <UIScrollViewDelegate>
@property(weak, nonatomic) UIScrollView * scrollView;
@property(weak, nonatomic) NSTimer * timer;
/** pageIndex */
@property(nonatomic, assign) NSInteger pageIndex;
@end
@implementation SNInfiniteScrollView
- (void)setImages:(NSArray<UIImage *> *)images {
_images = images;
//
self.pageIndex = 0;
//
[self updateContent];
//
[self startTimer];
}
/** . */
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//
UIScrollView * scrollView = [[UIScrollView alloc] init];
self.scrollView = scrollView;
scrollView.delegate = self;
// scroller
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
// scrollView
[self addSubview:scrollView];
//
for (int i = 0; i < ImageViewCount; i++) {
UIImageView * imageView = [[UIImageView alloc] init];
// .
imageView.contentMode = self.imageViewcontentMode;
[scrollView addSubview:imageView];
}
}
return self;
}
/** , */
- (void)layoutSubviews {
[super layoutSubviews];
self.scrollView.frame = self.bounds;
if (self.scrollDirection == ScrollDirectionVertical) {
self.scrollView.contentSize = CGSizeMake(0, ImageViewCount * self.bounds.size.height);
} else {
self.scrollView.contentSize = CGSizeMake(ImageViewCount * self.bounds.size.width, 0);
}
for (int i = 0; i < ImageViewCount; i++) {
UIImageView * imageView = self.scrollView.subviews[i];
if (self.scrollDirection == ScrollDirectionVertical) {
imageView.frame = CGRectMake(0, i * self.scrollView.frame.size.height, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
} else {
imageView.frame = CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
}
}
//
[self updateContent];
}
#pragma mark -
- (void)updateContent {
//
for (int i = 0; i < self.scrollView.subviews.count; i++) {
NSInteger pageIndex = self.pageIndex;
// imageView
UIImageView * imageView = self.scrollView.subviews[i];
if (i == 0) {
pageIndex--;
} else if (i == 2) {
pageIndex++;
}
if (pageIndex < 0) {
pageIndex = self.images.count - 1;
} else if (pageIndex >= self.images.count) {
pageIndex = 0;
}
// imageView tag
imageView.tag = pageIndex;
imageView.image = self.images[imageView.tag];
}
// // contentOffset
if (self.scrollDirection == ScrollDirectionVertical) {
self.scrollView.contentOffset = CGPointMake(0, scrollViewHeight);
} else {
self.scrollView.contentOffset = CGPointMake(scrollViewWidth, 0);
}
}
#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//
NSInteger page = self.pageIndex;
CGPoint point = CGPointZero;
for (int i = 0; i < self.scrollView.subviews.count; i++) {
UIImageView * imageView = self.scrollView.subviews[i];
point = [scrollView convertPoint:imageView.frame.origin toView:self.superview];
//=****** other way ****************** stone ***
if (self.scrollDirection == ScrollDirectionVertical) {
if (ABS(point.y - self.frame.origin.y) < 1.0) {
page = imageView.tag;
}
} else {
if (ABS(point.x - self.frame.origin.x) < 1.0) {
page = imageView.tag;
}
}
}
self.pageIndex = page;
self.pageControl.currentPage = page;
// [self updateContent];
//#warning mark - , ,
// [self updateContent]; // , ,
}
/** */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//
[self stopTimer];
}
/** */
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//
[self startTimer];
}
/** */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// , contentOffset
[self updateContent];
}
/** */ // ... contentOffset
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
//
[self updateContent];
}
#pragma mark -
- (void)startTimer {
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(next:) userInfo:nil repeats:YES];
// [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.timer = timer;
}
- (void)stopTimer {
[self.timer invalidate];
self.timer = nil;
}
- (void)next:(NSTimer *)timer {
if (self.scrollDirection == ScrollDirectionVertical) {
[self.scrollView setContentOffset:CGPointMake(0, 2 * self.scrollView.frame.size.height) animated:YES];
} else {
[self.scrollView setContentOffset:CGPointMake(2 * self.scrollView.frame.size.width, 0) animated:YES];
}
}
//=****** ****************** stone ***
+ (instancetype)scrollViewWithFrame:(CGRect)frame superView:(UIView *)superView images:(NSArray<UIImage *> *)images scrollDirection:(ScrollDirection)scrollDirection pageIndicatorTintColor:(UIColor *)pageIndicatorTintColor currentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor imageViewcontentMode:(UIViewContentMode)imageViewcontentMode {
//=****** scrollView ****************** stone ***
SNInfiniteScrollView * scrollView = [[SNInfiniteScrollView alloc] init];
scrollView.frame = frame;
scrollView.imageViewcontentMode = imageViewcontentMode;
scrollView.scrollDirection = scrollDirection;
//=****** image ****************** stone ***
scrollView.images = images;
//=****** pageControl ****************** stone ***
UIPageControl * pageControl = [[UIPageControl alloc] init];
scrollView.pageControl = pageControl;
pageControl.enabled = NO;
pageControl.currentPageIndicatorTintColor = currentPageIndicatorTintColor;
pageControl.pageIndicatorTintColor = pageIndicatorTintColor;
pageControl.numberOfPages = scrollView.images.count;
pageControl.bounds = CGRectMake(0, 0, scrollView.bounds.size.width, 44);
pageControl.center = CGPointMake(scrollView.bounds.size.width * 0.5, scrollView.bounds.size.height * 0.9);
[scrollView addSubview:pageControl];
[superView addSubview:scrollView];
//=************************ stone ***
return scrollView;
}
@end
이상 은 본문의 전체 내용 이 므 로 여러분 의 학습 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
PreferenceBundle에서 오른쪽 상단에 Respring 버튼을 클릭합니다.만나서 반갑습니다, Minazuki라고합니다. 프로필 이름 : Minazuki_dev Twitter : Repo : 아직 중학생이므로 말이 이상한 곳이 있습니다만 용서해 주세요… 🙏 theos (Mac이든 단품이든 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.