UIActivityIndicator as long as to implement function - ios

I want to use UIActivityIndicator on a function.
I'm implementing some Core Filters, some of which take almost a second to implement. I want that UIActivityIndicator to start and stop according the function.
I looked up online, but it's mostly using a timer. So that would make it hard-wired and not based on how long it actually takes to implement the function.
Can someone tell me a small example how I can do that ?

Declare ActivityIndicator
#property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
then,
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// Do any additional setup after loading the view, typically from a nib.
CGRect frame = CGRectMake (120.0, 185.0, 80, 80);
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[self.view addSubview:self.activityIndicatorView];
}
using this code you can start and stop the activityIndicator
[self.activityIndicatorView startAnimating]; //start
[self.activityIndicatorView stopAnimating]; //stop

all UI animation and navigation would be performed once the method execution is over. So if you want such functionality go for timer or dispatch queue.
as follows
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:self.window.frame];
[activity startAnimating];
//Your methods to be executed
double delayInSeconds = 0.01;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[activity stopAnimating];
});
in this method execution is kept in a queue after the execution of current method, method written dispatch_time_t will be executed.

Related

Change text of label in ios camera overlay UIPickercontroller

I'm trying to change though a timer in a different method the text of a label I created programmatically in a UIView overlay that goes on a UIImagePickerviewController, but of course when I try to change the text in this way
labelname.text = #"TEST";
I get the error "use of undeclared identifier labelname"
How can I refer to that specific label? Should I create a new label each time the timer ticks?
I tried to declare it in the .h file, but I'm guessing i'm just creating a different label...any ideas?
Just Pass the Label As a whole to the Timer Function:
Say Your Label is defined in ViewDidLoad like this :
- (void)viewDidLoad
{
//Do Something here
UILabel * label = [[UILabel alloc]
initWithFrame:CGRectMake(xcoordinateLabel, ycoordinateLabel, width, height)];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.textColor=[UIColor whiteColor];
label.text = #"TEST";
[self.view addSubview:label];
[self changeLabelText:label];
}
Where your changeLabelText is defined as this with some delay:
- (void) changeLabelText:(UILabel *)label
{
//Changing Values after 10s Delay
double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
label.text = #"TESTING Change";
});
}
Your Timer function can replace changeLabelText

Can't display UIActivityIndicator on top of mapview

In my code, I initialize a UIActivityIndicator, and add it as a subview, but it never appears. What's going wrong?
-(void) update {
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center=self.view.center;
[self.view addSubview:activityView];
[activityView startAnimating];
/** Stuff to access to server, this stuff works correctly.*/
[activityView stopAnimating];
}
As you are starting and stoping the activity as same runloop so it will not get change to update the UI.You are doing all server stuff on same synchronously than you are blocking the runLoop and do not giving time to activity indicator to start.You should start server stuff on next runLoop so activity indicator will get time to start.So use performSelector with delay 0.0 it will start the activity indicator and than perform your server stuff on immediate next runloop and than stop the activityInddicator
-(void) update {
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center=self.view.center;
[self.view addSubview:activityView];
[activityView startAnimating];
//Use performSelector with 0.0 so activity indicator starts
[self performSelector:#selector(thisWillStartAtNextRunLoop) withObject:nil afterDelay:0.0];
}
-(void)thisWillStartAtNextRunLoop{
/** Stuff to access to server, this stuff works correctly.*/
[activityView stopAnimating];
}
Edit execute it on main thread as all UI updation must be on main thread
Try
-(void) update {
dispatch_async(dispatch_get_main_queue(), ^{
UIActivityIndicatorView *activityView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center=self.view.center;
[self.view addSubview:activityView];
[activityView startAnimating];
});
/** Stuff to access to server, this stuff works correctly.*/
}

how to do the animate the uiimageview and stop the remaining process

I am doing one application.In that i am doing the animation for uiimageview to show the different image like below
UIImageView * fearthersanimate = nil;
[fearthersanimate setCenter:sender.center];
fearthersanimate = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120,200)];
[fearthersanimate setCenter:CGPointMake(sender.center.x, sender.center.y)];
fearthersanimate .animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"water_can_1.png"],
[UIImage imageNamed:#"water_can_2.png"],[UIImage imageNamed:#"water_can_3.png"],
[UIImage imageNamed:#"water_can_4.png"],[UIImage imageNamed:#"water_can_5.png"],
[UIImage imageNamed:#"water_can_6.png"],[UIImage imageNamed:#"water_can_7.png"],
[UIImage imageNamed:#"water_can_8.png"],
nil];
fearthersanimate.animationDuration = 1.0f;
fearthersanimate.animationRepeatCount = 1;
[self.view addSubview: fearthersanimate];
[fearthersanimate startAnimating];
But remaining operation is starting before end of this animation.But i need to do this animation first and until i need to stop the remaining process.
You can call your rest of operation in other method and call it after the same delay as you set animation time for images.
Below is the line of code by which you can call other method after some time interval
[self performSelector:#selector(restOfOperations) withObject:nil afterDelay:1.0];
//restOfOperations method defination/implementation
-(void)restOfOperations
{
//your code you want to perform after image animation
}
You can use the UIView animation block. For example:
[UIView animateWithDuration:0.5 animations:^{
} completion:^(BOOL finished) {
}];
Put the remaining process in the completion block.
Those animations are performed asynchronously, so you just need to fight async with async.
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(feathersanimate.animationDuration * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// Remaining logic
});

UIRefreshControl flicker in iOS7

If UIRefreshControl is started programmatically,(beginRefreshing and then endRefreshing ) when it is not visible, then there would be a continuous flicker in the UIRefreshControl animation on next manual refresh.
Add this method in UITableViewController subclass and do a pull to refresh after 2 seconds and you will see the flicker
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIRefreshControl * refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl = refreshControl;
[self.refreshControl beginRefreshing];
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self.refreshControl endRefreshing];
});
}
How to avoid this flicker?
Put your code in the viewDidLoad or viewWillAppear instead of viewDidAppear.
Also, can you tell me the reasoning behind putting the code inside the viewDidAppear method, rather than viewDidLoad or viewWillAppear?

Play Multiple animation in sequence

I am trying to execute ball blasting effect to play sequentially. One after another.
What I have done yet:
For ball blasting effect i had used
UIButton *ballButton = (UIButton *)[cell viewWithTag:10];
ballButton.imageView.animationImages = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
nil];
ballButton.imageView.animationDuration = 1;
ballButton.imageView.animationRepeatCount = 1;
and this line on code is attached to multiple buttons in a cell of a collection view.
I call these ballbutton.imageview start the animation like this
[UIView animateWithDuration:1 delay:5 options:UIViewAnimationOptionCurveEaseOut animations:^{
NSIndexPath *path2 = [NSIndexPath indexPathForRow:x inSection:0];
UICollectionViewCell *cell = [ballContainer cellForItemAtIndexPath:path2];
UIButton *ballObject = (UIButton *) [cell viewWithTag:10];
[ballObject.imageView startAnimating];
} completion:^(BOOL b){
NSLog(#" here i call next animation of ball blast to execute ");
}];
I nested 3 animations buttons like this.
First of all, why don't you create one big animation for all other three? The thing with the UIView animateWithDuration: is that it performs the animation block in the period of time you gave to it, i.e. setting the frame from (200,200) to (0,0) will move it proportionally in the time of a second, in your case. But UIImageView's properties regarding animations are made in such way that the animation is already done for you.
Personally, I would suggest using a timer, like this:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:ballObject.imageView.animationDuration
target:self
selector:#selector(performNextAnimation:)
userInfo:nil repeats:NO];
[ballObject.imageView startAnimating];
And in the performNextAnimation method:
- (void) performNextAnimation{
[timer invalidate]; // you have to access the timer you've scheduled with the animation
timer = nil;
/* code for starting the next animation */
}
this way i solved my issue.
initially i started animation by calling this
[self startAnim:index];
than implemented this StartAnim like this and problem solved.
-(void)StartAnim :(int)x{
NSIndexPath *path2 = [NSIndexPath indexPathForRow:x inSection:0];
UICollectionViewCell *cell = [ballContainer cellForItemAtIndexPath:path2];
UIButton *ballObject = (UIButton *) [cell viewWithTag:10];
[ballObject setBackgroundImage:nil forState:UIControlStateNormal];
ballObject.imageView.image = nil;
[ballObject.imageView startAnimating];
double delayInSeconds = 0.15;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if(x-6>=0){
[self StartAnim :(x-6)];
}
});
}

Resources