使用物镜c以5秒的时间间隔连续移动10张图像?

时间:2012-01-04 09:41:45

标签: iphone

我有10张图片,我希望以两种方式连续显示10张图片,一张是通过用户互动移动图片,另一种是通过在一段时间间隔内自动移动图片而不进行任何用户交互。以下代码允许我在用户触摸并移动图像时移动图像....但是如何自动移动图像?

{     scrollView.delegate = self;

[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];

scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;

NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
    NSString *imageName = [NSString stringWithFormat:@"pimac%d.gif", (nimages + 1)];
    UIImage *image = [UIImage imageNamed:imageName];
    if (image == nil) {
        break;
    }
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    CGRect rect = imageView.frame;
    rect.size.height = image.size.height;
    rect.size.width = image.size.width;
    rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
    rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);

    imageView.frame = rect;

    [scrollView addSubview:imageView];
    [imageView release];

    cx += scrollView.frame.size.width;
}

self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];

}

2 个答案:

答案 0 :(得分:0)

你可以使用NSTimmer并在那里调用你的图像移动功能。

self.timer = [NSTimer scheduledTimerWithTimeInterval:5
                                           target:self
                                         selector:@selector(imageMove) 
                                         userInfo:nil
                                          repeats:YES];

答案 1 :(得分:0)

const CGFloat kScrollObjWidth   = 320.0;
const NSUInteger kNumImages = 3;

-(void)viewdidLoad
{
// additional further step
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
curXLoc=0;
}


- (void) onTimer {

   curXLoc += kScrollObjWidth; 

    //This makes the scrollView scroll to the desired position  
    if(curXLoc<(kNumImages * kScrollObjWidth))
   ScrollView.contentOffset = CGPointMake(curXLoc,0 );  
    else
    {
        curXLoc =0; 
         ScrollView.contentOffset = CGPointMake(curXLoc,0 );
    }

}