Animated dots in loading text - ios7

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.

Related

UILabel to appear for 3 seconds after UIButton is pressed

After I hit a pause button, I want a label to appear until the user taps on the screen. How do I do this?
So far I have this
- (IBAction)ButtonPausePressed:(id)sender {
PauseLabel.hidden = false (//how do i make it only visible until user taps?//)
if (GameEnd != true){
if ([GameUpdate isValid]){
[GameUpdate invalidate];
[BirdUpdate invalidate];
}else{
BirdUpdate = [NSTimer scheduledTimerWithTimeInterval:0.015
target:self
selector:#selector(UpdateBird)
userInfo:nil
repeats:YES];
GameUpdate = [NSTimer scheduledTimerWithTimeInterval:0.025
target:self
selector:#selector(GameUpdate)
userInfo:nil
repeats:YES];
}
}
}
- (IBAction)ButtonPausePressed:(id)sender {
PauseLabel.hidden = false
[self performSelector:#selector(hiddenLabel) withObject:nil afterDelay:3];
...
}
- (void)hiddenLabel{
PauseLabel.hidden = YES;
}
Try following
[your_view addSubview:your_label];
your_label.hidden = YES;
[your_label performSelector:#selector(setHidden:) withObject:#NO afterDelay:3];
Try like this:
-(void)viewDidLoad
{
UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(btnSingleClicked:)];
[singleTapGesture setDelegate:self];
singleTapGesture.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTapGesture];
}
-(void)btnSingleClicked:(UITapGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateEnded && !self.PauseLabel.hidden)
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView animateWithDuration:3.0f // 3 sec to hide it
delay:0.0f
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.PauseLabel setAlpha:0.0f];
}
completion:^(BOOL finished)
{
// remove from super view if needed.
// Now you can handle touch events etc
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}
The idea behind the line is add a single tap gesture on your viewcontroller's view and if the pause label is displayed and a touch is happend on view check its state is ended and hide label with animation.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
PauseLabel.hidden = YES;
});

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*/}];

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.

Pausing between image views

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.

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;
...
}