English 中文(简体)
国际职工会发展变化观点 国家安全局
原标题:Change View in UIScrollView with NSTimer

我正在使用以下代码,以建立一个<条码>。 观点:

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {

    CGFloat yOrigin = i * self.view.frame.size.width;
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [scroll addSubview:awesomeView];

}

scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

[self.view addSubview:scroll];

问题在于:我如何实施<条码>NStimer,以便每4秒更换一页?

    [NSTimer scheduledTimerWithTimeInterval:4.0f
                                 target:self
                               selector:@selector(updateCounter:)
                               userInfo:nil
                                repeats:YES];

我正在努力撰写最新的宣传方法。 请帮助

- (void)updateCounter:(NSTimer *)theTimer {
 NSInteger numberOfViews = 3;
     int i = 0; i++;
     if (i < numberOfViews) {

    yOrigin = i * self.view.frame.size.width;
    [scroll scrollRectToVisible:CGRectMake(yOrigin, 0, self.view.frame.size.width,      self.view.frame.size.height) animated:YES];
}

return; }

OK, so with this code, NSTimer changes from view 1 to view 2, but it stops, it does not change after the next 4 seconds to view 3. What I want to do is to change from view 1, to view 2, after 4 seconds, then to view 3, after 4 seconds, and so on. I want to have as many views as I need.

最佳回答

你们正在竭尽全力。 现在需要执行<代码>更新。 以便发展 观点自动出现。 保持全球宽度。 每次打电话到<编码>更新。 您可以这样做。

int global_width  = self.view.frame.size.width;
int global_height = self.view.frame.size.height;
- (void) updateCounter
{
    int width = (global_width%self.view.frame.size.width)*self.view.frame.size.width;
    [yourScrollView scrollRectToVisible:CGRectMake(0, 0, width, global_height) animated:YES];

    global_width  += self.view.frame.size.width;
    return;
}

这样,你就可以自动地在网页上穿梭。 所有这些作品都是免费的。 但这一概念是正确的。

问题回答

如果您希望每四秒更新一次反响,请您介绍最新情况 以这种方式对抗:

[NSTimer scheduledTimerWithTimeInterval:4.0f
                                 target:self
                               selector:@selector(updateCounter)
                               userInfo:nil
                                repeats:YES];

There s two differences in what you did. #1, the time interval is 4 seconds. #2, if you don t have any parameters going into the updateCounter method, don t append a colon to the selector.

之后是更新的。 反制方法:

- (void) updateCounter
{
    // what view are you modifying?
    // the content view or a subview within the ScrollView s contentView ?   
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签