我的应用程序需要在视图控制器中处于非活动状态2分钟后显示屏幕保护程序。我用了定时器和敲击手势识别器来实现它,它可以工作,但图像不能滚动。它只显示一个图像。
NSUInteger pg = 0;
-(void)viewDidLoad{
timer= [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(displayAlert) userInfo:nil repeats:YES];
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(resetTimer:)];
self.view.userInteractionEnabled = true;
tap1.delegate=self;
tap1.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap1];
}
-(void)resetTimer:(UITapGestureRecognizer *)recognizer
{
[timer invalidate];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
[timer invalidate];
timer = nil;
[pageControl removeFromSuperview];
[ScrollImages removeFromSuperview];
timer= [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:@selector(displayAlert) userInfo:nil repeats:YES];
return YES;
}
-(void)displayAlert
{
//Display Screensaver images in a scrollview using pagecontrol
ScrollImages=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 5, 0, 0)];
ScrollImages.showsVerticalScrollIndicator=NO;
ScrollImages.showsHorizontalScrollIndicator=NO;
ScrollImages.delegate = self;
ScrollImages.scrollEnabled = YES;
ScrollImages.backgroundColor=[UIColor clearColor];
[self.view addSubview:ScrollImages];
int xOffset = 0;
if(xOffset>=0){
ScrollImages.frame=CGRectMake(0, 70, self.view.frame.size.width, self.view.frame.size.height);
ScrollImages.scrollEnabled=true;
[ScrollImages setPagingEnabled:true];
for (int i=0; i< [arrScreenSaverImages count]; i++){
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width*i,0,self.view.frame.size.width, self.view.frame.size.height)];
NSURL *url = arrImages[i];
NSLog(@"%@", url);
NSString *urlString=[NSString stringWithFormat:@"%@",url];
[img sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil options:SDWebImageRefreshCached];
ScrollImages.contentSize = CGSizeMake(ScrollImages.frame.size.width*[arrScreenSaverImages count],350);
[ScrollImages addSubview:img];
}
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(0, ScrollImages.frame.origin.y+ScrollImages.frame.size.height-50, self.view.frame.size.width, 30);
pageControl.numberOfPages = [arrImages count];
pageControl.currentPage = 0;
[pageControl addTarget:self action:@selector(changepage:) forControlEvents:UIControlEventValueChanged];
pageControl.backgroundColor=[UIColor clearColor];
if ([pageControl respondsToSelector:@selector(setPageIndicatorTintColor:)]){
pageControl.pageIndicatorTintColor = [UIColor whiteColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
}
[self.view addSubview:pageControl];
}
CGRect frame = ScrollImages.frame;
frame.origin.x = frame.size.width * pg;
frame.origin.y = 0;
[ScrollImages scrollRectToVisible:frame animated:YES];
pg++;
if (pg == arrScreenSaverImages.count+1){
pg = 0;
}
[timer invalidate];
timer = nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int page = ScrollImages.contentOffset.x / ScrollImages.frame.size.width;
pageControl.currentPage = page;
}
如何使图像滚动?任何想法/建议都将对有所帮助
每次计时器触发时,您都要添加ScrollImages
!
解决方案:
- (void)displayAlert {
if (ScrollImages!=nil)
return