UIScrollView 자동 순환 스크롤 실현
5299 단어 apple
2.
//
// HomeView.m
// Direction
//
// Created by apple on 12-3-22.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "HomeView.h"
#define WIDTH_OF_SCROLL_PAGE 320
#define HEIGHT_OF_SCROLL_PAGE 460
#define WIDTH_OF_IMAGE 320
#define HEIGHT_OF_IMAGE 284
#define LEFT_EDGE_OFSET 0
@implementation HomeView
@synthesize scrollView;
- (void)dealloc {
[scrollView release];
[myTimer release];
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:[self homeScrollView]];
}
return self;
}
-(UIScrollView *) homeScrollView{
scrollView = [[UIScrollView alloc] init];
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;
scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
slideImages = [[NSMutableArray alloc] init];
[slideImages addObject:@"image1.png"];
[slideImages addObject:@"image2.png"];
[slideImages addObject:@"image3.png"];
//add the last image first
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]];
imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
for (int i = 0;i<[slideImages count];i++) {
//loop this bit
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
}
//add the first image at the end
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
[scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(0, 0)];
[self addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
[self performSelector:@selector(updateScrollView) withObject:nil afterDelay:0.0f];
return self.scrollView;
}
- (void) updateScrollView
{
[myTimer invalidate];
myTimer = nil;
//time duration
NSTimeInterval timeInterval = 3;
//timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self
selector:@selector(handleMaxShowTimer:)
userInfo: nil
repeats:YES];
}
- (void)handleMaxShowTimer:(NSTimer*)theTimer
{
CGPoint pt = scrollView.contentOffset;
int count = [slideImages count];
if(pt.x == WIDTH_OF_IMAGE * count){
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:YES];
}else{
[self.scrollView scrollRectToVisible:CGRectMake(pt.x+WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:YES];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width / ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;
if (currentPage==0) {
//go last but 1 page
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
} else if (currentPage==([slideImages count]+1)) { // +1,
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
M1 및 React Native(두통이 아님)React Native에서 작업했거나 작업한 사람이라면 iOS 설정, 개발 및 배포가 Android보다 훨씬 더 어렵다는 것을 부인할 수 없습니다. M1이 기술의 경이로움에 다름 아니라는 사실을 부인할 수 없으며 우...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.