Pausing between image views - cocoa-touch

I want to run a series of pictures with dialog bubble (like a strip cartoon). I've tried to use the action:
-(IBAction) runDialog:(id)sender {
imageView.image = [UIImage imageNamed:#"a1.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"b1.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"b2.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"a2.png"];
[NSThread sleepForTimeInterval:5.0];
}
This doesn't work. All it does is show the last image (a2.png) After About 20 seconds
Any ideas how I should go about this:showing a series of pictures with a pause in between?

Use NSTimer instead.
- (IBAction)runDialog:(id)sender {
yourInstanceVariableOfNSTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self selector:#selector(showNextPage) userInfo:nil repeats:NO];
}
- (void)showNextPage {
imageView.image = [yourInstanceVariableOfNSArray
objectAtIndex:++yourInstanceVariableOfNSInteger];
if (!transitionsFinished) {
yourInstanceVariableOfNSTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self selector:#selector(showNextPage) userInfo:nil repeats:NO];
}
}

A simpler way to handle this would be to use a UIImageView in the dialog and set its animationImages, animationDuration, etc... properties.

Related

Animated dots in loading text

I want to do loading animation. When I tap the button text become "loading" then "loading." then "loading.." and from the beginning. How to do this?
I tried [UIView animateWithDuration] but it doesn't help.
It's not very difficult to achieve. Create a NSTimer property and then the start/stop/update methods like so:
- (void)startLoadingAnimation {
[self.myButton setTitle:#"Loading" forState:UIControlStateNormal];
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:#selector(updateLoadingLabel) userInfo:nil repeats:YES];
}
- (void)updateLoadingLabel {
NSString *ellipses = #"...";
if ([self.myButton.titleLabel.text rangeOfString:ellipses].location == NSNotFound) {
[self.myButton setTitle:[NSString stringWithFormat:#"%#.",self.myButton.titleLabel.text] forState:UIControlStateNormal];
} else {
[self.myButton setTitle:#"Loading" forState:UIControlStateNormal];
}
}
- (void)endLoadingAnimation {
[self.myTimer invalidate];
self.myTimer = nil;
[self.myButton setTitle:#"Loading" forState:UIControlStateNormal];
}
In this example the label is updated every 0.5 seconds.

How to elegantly transition images in slide show?

After some research on here I found a solution to creating a slide show of images in an iphone app. All working fine, currently the images show one after another.
My question is, can I make the images cross dissolve/fade rather than just appearing, and if so could I get some advice on that.
My Code
.m
}
int topIndex = 0, prevTopIndex = 1;
- (void)viewDidLoad
{
imagebottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagebottom];
imagetop = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,160,240)];
[self.view addSubview:imagetop];
imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"ip2.png"],
nil];
NSTimer *timer = [NSTimer timerWithTimeInterval:5.0
target:self
selector:#selector(onTimer)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer fire];
[super viewDidLoad];
}
-(void)onTimer{
if(topIndex %2 == 0){
[UIView animateWithDuration:5.0 animations:^
{
imagebottom.alpha = 0.0;
}];
imagetop.image = [imageArray objectAtIndex:prevTopIndex];
imagetop.image = [imageArray objectAtIndex:topIndex];
}else{
[UIView animateWithDuration:5.0 animations:^
{
imagetop.alpha = 1.0;
}];
imagetop.image = [imageArray objectAtIndex:topIndex];
imagebottom.image = [imageArray objectAtIndex:prevTopIndex];
}
prevTopIndex = topIndex;
if(topIndex == [imageArray count]-1){
topIndex = 0;
}else{
topIndex++;
}
You have a bunch of options. If you have a "container" view, you can make a new UIImageView transparent (alpha = 0), then use a UIView animation block to fade one image in and the other out (or leave alpha = 1 on both and slide one in from any side you want.
For instance, you have your main view, self.view. You have one UIImageView *oldView, that is now at rect (0,0,320,100) and you want to slide it to the right as you slide a newView imageView in. First you set the newView frame to (-320,0,320,100) then [self.view addSubview newView]. To animate the change:
[UIView animateWithDuration:2 animations:^
{
oldView.frame = CGRectMake(320, 0, 320, 100);
newView.frame = CGRectMake(0,0,320, 100);
}
completion:^(BOOL finished)
{
[oldView removeFromSuperView];
} ];
You also have the option of using UiView's
+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
which gives you more/different options (and its less work too!). For instance, using the same basic objects in the first example, but the newView having the same frame as the oldView:
transitionFromView:oldView toView:newView duration:2 options: UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished)) { /*whatever*/}];

Wrong return of UIImageView.isAnimating?

I have a Problem with using the isAnimating function.
I have got an UIImageView called imgBecher.
In the viewDidLoad function I play a small Animation:
imgBecher.image = [UIImage imageNamed:#"diceholderAni.11.png"];
AniArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"diceholderAni.1.png"],
[UIImage imageNamed:#"diceholderAni.2.png"],
[UIImage imageNamed:#"diceholderAni.3.png"],
[UIImage imageNamed:#"diceholderAni.4.png"],
[UIImage imageNamed:#"diceholderAni.5.png"],
[UIImage imageNamed:#"diceholderAni.6.png"],
[UIImage imageNamed:#"diceholderAni.7.png"],
[UIImage imageNamed:#"diceholderAni.8.png"],
[UIImage imageNamed:#"diceholderAni.9.png"],
[UIImage imageNamed:#"diceholderAni.10.png"],
nil];
imgBecher.animationImages = AniArray;
[imgBecher setAnimationRepeatCount:1];
imgBecher.animationDuration = 1;
[imgBecher startAnimating];
Everything fine so far.
Now I call a Loop with:
[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:#selector(gameLoop) userInfo:nil repeats:YES];
-(void)gameLoop {
NSLog(#"%i",imgBecher.isAnimating);
}
Now gameLoop fires 1 all over the time, not seeing that the animation is over.
BUT
as soon as I touch the screen anywhere, it fires 0, just as it should be.
I need this later on, where I have 4 animations, which should be played right after another without any user actions.
Thanks in advance! ;)
Probably you have to use userInteractionEnabled property:
imgBecher.userInteractionEnabled = NO; or self.view.userInteractionEnabled = NO;

Display an image for 1 sec on the view

What I want to realize is:
When I touch a button, the image display on the view for 1 sec, then the image disappear.
I know that a NSTimer will help, but i dont know how to write the right code...need your help, thanks.
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showPictures:) userInfo:nil repeats:NO];
}
- (void)showPictures:(NSTimer *)timer {
bodytouchedimage.hidden = YES;
}
What you should to is call the showPictures function when you touch the button and then within the showPictures method you add a NSTimer that will call a hidePictures method 1 second later
- (void)showPictures
{
bodytouchedimage.hidden = NO;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hidePictures) userInfo:nil repeats:NO];
}
- (void)hidePictures
{
bodytouchedimage.hidden = YES;
}
Rather than using NSTimer, it would be easier to simply call your method to hide the image like this:
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
[self performSelector:#selector(hidePicture) withObject:nil afterDelay:1];
}
- (void)hidePicture {
bodytouchedimage.hidden = YES;
}
performSelector:withObject:afterDelay: is a method of the NSObject class.

Objective C: Passing variables between classes

I want to pass an NSMutableDictionary from one class through a timer to another class that is called from the timer. I get an error at #selector([generateStickFig:enemies]) I've tried taking [] out but it still errs.
-(void)awakeFromNib {
//start timer that fires every second
NSMutableDictionary *enemies = [[NSMutableDictionary alloc] init];
[NSTimer scheduledTimerWithTimeInterval:(0.001) target:self selector:#selector([generateStickFig:enemies]) userInfo:nil repeats:YES];
}
-(void) generateStickFig:(NSMutableDictionary *)enemies {
int x = random() % 1000;
if (x == 1) {
stickFig = [[UIButton alloc] initWithFrame:CGRectMake(0, 650, 50, 50)];
[stickFig setBackgroundColor:[UIColor blackColor]];
[stickFig addTarget:self action:#selector(tapFig:) forControlEvents:UIControlEventTouchUpInside];
[enemies setObject:object forKey:[NSString stringWithFormat:#"object%i",i]];
[self.view addSubview:stickFig];
}
}
Any suggestions?
Try
[NSTimer scheduledTimerWithTimeInterval:(0.001) target:self selector:#selector(generateStickFig:) userInfo:enemies repeats:YES];
to create the timer. Then change the callback method to
-(void) generateStickFig:(NSTimer *)timer {
NSDictionary *enemies = (NSDictionary *)timer.userInfo;
...
}