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
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:
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. ;)
NSThread class reference
UIScrollView class reference
NSTimer class reference
Stack Overflow – website that allows you to ask and respond to various questions, devoted to the programming issues.
iPhone / iPod Touch / iPad developer Community in Ukraine