Katerina Trofimenko

Katerina Trofimenko

Faculty:
Computer Sciences and Technologies (CST)

Speciality:
"Information Control Systems and Technologies" (ICS)

Department:
Automated Control Systems (ACS)

Theme of Master's Work:
"Development of an automated system of testing students' knowledge in the field of computer technology with the use of adaptation mechanisms"

Scientific Supervisor:
Dr.sc.sciences, prof., Skobtsov Yuriy

Using NSTimer with UIScrollView.

NSTimer – class, to create timers. After a set interval it sends the message object.

UIScrollView – allows to scroll content, larger than the display.

However, if you use these 2 objects at the same time, there are some, at first glance inconspicuous features...


So, let's begin, create a simple ViewBasedProject, called TimerScroll2.

Create a label redTimerLabel, variable redTime, which will be stored current time value, and the variable timeStamp, which will be stored value of the interval, through which will change the label text.

ScrollTimer2ViewController.h

@interface ScrollTimer2ViewController : UIViewController {

UILabel *redTimerLabel;

float redTime;

float timeStamp;

}

ScrollTimer2ViewController.m

- (void)viewDidLoad {

[super viewDidLoad];

redTimerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];

redTimerLabel.backgroundColor = [UIColor redColor];

redTime = 0;

timeStamp = 1.0;

[self.view addSubview:redTimerLabel];

}

Now create a timer. It has the following options:

  1. timeInterval – the time after which the method will called
  2. target – reference to the object
  3. selector – the method declaration
  4. userInfo – an additional parameter
  5. repeats – a flag indicating whether the timer is triggered several times

ScrollTimer2ViewController.m

-(void) startTimer {

[NSTimer scheduledTimerWithTimeInterval:timeStamp

target:self

selector:@selector(setRedLabelText)

userInfo:nil repeats:YES];

}

Timer refers to the method setRedLebelText. Each call of the methof modifies the text of the label.

ScrollTimer2ViewController.m

-(void) setRedLabelText {

redTime+=timeStamp;

NSString *str = [[NSString alloc] initWithFormat:@"%.2f", redTime];

redTimerLabel.text = str;

[str release];

}

Start:

ScrollTimer2ViewController.m

- (void)viewDidLoad {

.....

[self startTimer];

}

And everything works fine:).

Now, we'll add a UIScrollView.

TimerScrollView.h

@interface TimerScrollView : UIScrollView {

UIImageView *imageView;

}

@end

TimerScrollView.m

@implementation TimerScrollView

- (id)initWithFrame:(CGRect)frame {

if ((self = [super initWithFrame:frame])) {

self.scrollEnabled = YES;

self.delegate = self;

self.minimumZoomScale = 0.1f;

self.maximumZoomScale = 5.0f;

imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image0.jpg"]];

[self addSubview:imageView];

[self setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];

}

return self;

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

return imageView;

}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale { }

- (void)dealloc {

[super dealloc];

}

@end


ScrollTimer2ViewController.m

- (void)viewDidLoad {

.....

TimerScrollView *sv = [[TimerScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];

[self.view addSubview:sv];

[self.view addSubview:redTimerLabel];

[self startTimer];

}

Run and scroll. Here's the problem arises: in order to smooth the image which is moved, most of the resources given to the animation of the UIScrollView. And all the timers are suspended.

The problem is solved by an additional thread:

ScrollTimer2ViewController.m

-(void) startTimer {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[NSThread detachNewThreadSelector:@selector(launchTimer) toTarget:self withObject:nil];

[pool release];

}


-(void)launchTimer{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[NSThread setThreadPriority:1.0];

continuePlaying = YES;

while (continuePlaying)

{

[NSThread sleepForTimeInterval:timeStamp];

[self performSelectorOnMainThread:@selector(setRedLabelText) withObject:nil waitUntilDone:NO];

}

[pool release];

}

So, now everything works as needed.

Test project can be downloaded here


Below, video of a real project, the decision was used. ;)

Links

  1. http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSThread_Class/Reference/Reference.html

    NSThread class reference

  2. http://developer.apple.com/iphone/library/documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html

    UIScrollView class reference

  3. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html

    NSTimer class reference

  4. http://stackoverflow.com/questions/605027/uiscrollview-pauses-nstimer-until-scrolling-finishes

    Stack Overflow – website that allows you to ask and respond to various questions, devoted to the programming issues.

  5. http://iphonedevcamp.com.ua/

    iPhone / iPod Touch / iPad developer Community in Ukraine