I am trying to start imageView's animation on viewDidAppear phase.
I would like to stop animation when content of webview loaded on viewDidLoaded phase.
However my UIImageView does not stop animation when stopAnimation is called
on ViewDidLoaded phase.
can anybody tell me what should I do to stop animation on viewDidLoaded?
any help will be appreciated.
UIImageView *customActivityIndicator;
-(void)viewDidAppear:(BOOL)animated{
...
customActivityIndicator = [[UIImageView alloc] initWithFrame:CGRectMake(loadingPosX, loadingPosY, loadingImageWidth, loadingImageHeight)];
[self.view addSubview: customActivityIndicator];
customActivityIndicator.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"],[UIImage imageNamed:#"2"],[UIImage imageNamed:#"3.png"],[UIImage imageNamed:#"4.png"],[UIImage imageNamed:#"5.png"],nil];
customActivityIndicator.animationDuration = 1.0; // in seconds
customActivityIndicator.animationRepeatCount = 0; // sets to loop
[customActivityIndicator startAnimating]; // starts animating
}
- (void)viewDidLoad {
[super viewDidLoad];
....
[webView loadRequest : requestObj];
[customActivityIndicator stopAnimating]; // stop animating
}
Use the UIWebViewDelegate and protocol method webViewDidFinishLoad
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[customActivityIndicator stopAnimating]; // stop animating
}
- (void)viewDidLoad {
[super viewDidLoad];
....
webView.delegate = self;
[webView loadRequest : requestObj];
}
Related
I have this file that I'm trying to reload but when I reload it I get the screen that is supposed to show up and then a blank screen until I hit reload again. I'm not sure why I'm getting this blank screen. Also, I do not get anything from
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {}
nor
-(void)didReceiveMemoryWarning
Here's my viewController.m file I have the index.html file in the same directory as it does load once but I'm not sure if webkit is crashing or where
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
#interface ViewController ()
#end
#implementation ViewController
NSString *localURL;
NSURLRequest *urlRequest;
- (void)viewDidLoad {
[super viewDidLoad];
JSContext *ctx = [[JSContext alloc] init];
ctx[#"console"][#"log"] = ^(NSString *message) {
NSLog(#"Javascript log: %#", message);
};
// Do any additional setup after loading the view, typically from a nib.
// [UIView setAnimationsEnabled:NO];
//_webView = [[WKWebView alloc] initWithFrame:self.view.frame];
// _webView.navigationDelegate = self.webView ;
// _webView.UIDelegate = self;
//[self.view addSubview:_webView ];
_webView= [[WKWebView alloc] initWithFrame:CGRectMake(0, 100, 500, 500)];
localURL = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:localURL]];
[_webView loadRequest:urlRequest];
[self.view addSubview:_webView];
//button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(reload) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Reload" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 20.0, 100.0, 50.0);
button.backgroundColor = UIColor.blackColor;
[self.view addSubview:button];
}
- (void)webViewWebContentProcessDidTerminate:(WKWebView *)webView {
// Reload current page, since we have crashed the WebContent process
// (most likely due to memory pressure)
NSLog(#"most likely due to memory pressure reloaded");
[webView reload];
}
-(void)reload{
NSLog(#"reloading now");
[self.webView loadRequest:urlRequest];
//[self.webView stopLoading];
// [self loadView];
//[_webView loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
NSLog(#"memory issue");
}
//- (IBAction)rButton:(UIButton *)sender {
// NSLog(#"Well we didnt crash");
// [self.webView reload];
//}
#end
If the problem is due to a termination of the web the delegate function webViewWebContentProcessDidTerminate will be called.
In your case it looks that you didn't set the viewController as the navigationDelegate of the webview:
webview.navigationDelegate = self
I'm trying to add a subview to view that displays an activity indicator and blocks touches while I'm fetching data from the server.
Here is the code that adds the loading view to the main view controller's screen:
+ (void)showLoadingView {
ViewController *vc = [AppDelegate mainViewController];
if (!vc._activityViewContainer) {
LoadingView *loadingView = [[LoadingView alloc] initWithFrame:vc.view.bounds];
[vc.view addSubview:loadingView];
vc._activityViewContainer = loadingView;
}
}
+ (void)stopLoadingView {
ViewController *vc = [AppDelegate mainViewController];
if (vc._activityViewContainer) {
[vc._activityViewContainer removeFromSuperview];
vc._activityViewContainer = nil;
}
}
Here is my loading view:
#implementation LoadingView
- (id)initWithFrame:(CGRect)frame {
if (self == [super initWithFrame:frame]) {
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor lightGrayColor];
self.alpha = 0.6;
self.layer.zPosition = 10000;
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
NSUInteger avSize = 100;
CGRect activityViewFrame = CGRectMake((frame.size.width - avSize) / 2, (frame.size.height - avSize) / 2, avSize, avSize);
activityView.frame = activityViewFrame;
[self addSubview:activityView];
[activityView startAnimating];
}
return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
return [self pointInside:point withEvent:event] ? self : nil; }
#end
I'm trying to make it so that the loading view absorbs/blocks all touches to other sibling views. It's working fine when I'm testing it by presenting it without doing any fetching, it enters the loading view's hitTest method and blocks the touch. However, when I'm actually doing any kind of fetch from the server, it doesn't seem to work. The touch is delayed a few seconds and does not get intercepted by the LoadingView.
I'm using GTMHTTPFetcher's
- (BOOL)beginFetchWithCompletionHandler:(void (^)(NSData *data, NSError *error))handler
I'm not that sure what's going on and I tried looking for similar examples on SO, but couldn't find what I was looking for. Any help would be great, thanks!
I was doing my fetches on the main thread which was blocking the UI; I thought GTM did everything on a different thread, but it doesn't. Thanks to those who posted things which helped me in the right direction
I am having some trouble getting my UIActivityIndicatorView to start animating. Here is my setup:
In my viewDidLoad in my view controller I have:
- (void)viewDidLoad{
schoolList = NO;
_activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_activityIndicator startAnimating];
[NSThread detachNewThreadSelector: #selector(getSchoolList) toTarget: self withObject: nil];
[self performSelector:#selector(updateUI) withObject:nil afterDelay:20.0];
[super viewDidLoad];
}
The selector getSchoolList communicates with a server to retrieve a list of schools in a given state. Then, the selector updateUI is called to populate my UIPickerView with the list. In my updateUI selector I have:
-(void)updateUI {
_schools = [_server returnData];
if(!(_schools == nil)) {
NSLog(#"update the UI");
}
else
NSLog(#"Error:Show re-load button");
[_activityIndicator stopAnimating];
}
When I run this code, my UIActivityIndicatorView shows up, but does not animate. Can someone explain the proper way to animate my UIActivityIndicatorView? Any help is much appreciated.
You need to add the UIActivityIndicatorView to your view in viewDidLoad like this:
- (void)viewDidLoad {
schoolList = NO;
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self addSubview:_activityIndicator];
[_activityIndicator startAnimating];
[NSThread detachNewThreadSelector: #selector(getSchoolList) toTarget: self withObject: nil];
[self performSelector:#selector(updateUI) withObject:nil afterDelay:20.0];
[super viewDidLoad];
}
EDIT
If _activityIndicator is a properly connected IBOutlet to a UIActivityIndicatorView, you should only need to check the 'animating' box. There would be no need to alloc/init another UIActivityIndicatorView.
Breakpoint the update function, but I don't see where you add that as a view to the hierarchy. I think you're looking at a different indicator view in the program.
Any reason for this error "CGAffineTransformInvert"
Should I be worried?
I have a .xib with a view, and 4 webViews located outside of the view but within the same xib. Then in the code I add the webViews as subviews to a scroll view inside the view. Would that cause the problem?
Code is below:
//Called first to initialize this class. Also, initializes the nib file and tab bar name.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"More", #"More");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
//Initialize the more tab titles and views
-(void)initViewsandTitles{
MoreTabPages = [NSArray arrayWithObjects:self.aboutWebView,
self.newsUpdateWebView,
self.feedbackWebView,
self.creditsResourceWebView, nil];
titles = [[NSArray alloc] initWithObjects:#"About Locavore",
#"News and Updates",
#"Feedback",
#"Credits and Resources", nil];
}
//Initialize the URLs
-(void)initURLs{
websites = [[NSArray alloc] initWithObjects:#"http://www.getlocavore.com/",
#"http://twitter.com/enjoy_locavore",
#"https://getsatisfaction.com/localdirt/products/localdirt_locavore",
#"http://www.getlocavore.com/about", nil];
}
//Called after the controller's view is loaded into memory.
- (void)viewDidLoad
{
[super viewDidLoad]; //Call the super class init method
[self setupSpinner]; //Start the spinner animatio
[self initViewsandTitles]; //Initialize the views and titles
[self initURLs]; //Initialize the URLs
[self setScrollandPageViewProperties]; //Set the scroll and page view properties
[self setUpPageViews]; //Create the web pages
}
//UIScrollViewDelegate Protocol Reference. Called whn the user scrolls the content within the reciever
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.MoreTabScrollView.frame.size.width;
int page = floor((self.MoreTabScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.MoreTabPageControl.currentPage = page;
self.MoreTabTitle.text = [titles objectAtIndex:page];
}
}
//UIScrollViewDelegate Protocol Reference. Called when the scroll view is about to start scolling content
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
//UIScrollViewDelegate Protocol Reference. Called when the scroll view has ended decelerating the scrolling movement
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(#"DID END SCROLLING");
pageControlBeingUsed = NO;
}
//Called when the page control value changes
- (IBAction)MoreTabChangePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.MoreTabScrollView.frame.size.width * self.MoreTabPageControl.currentPage;
frame.origin.y = 0;
frame.size = self.MoreTabScrollView.frame.size;
[self.MoreTabScrollView scrollRectToVisible:frame animated:YES];
self.MoreTabTitle.text = [titles objectAtIndex:self.MoreTabPageControl.currentPage];
// Keep track of when scrolls happen in response to the page control
// value changing. If we don't do this, a noticeable "flashing" occurs
// as the the scroll delegate will temporarily switch back the page
// number.
pageControlBeingUsed=YES;
}
//Create a frame for each page and add the page to the scroll view
-(void)setUpPageViews{
//Set up all page views for the more tab
for (int i = 0; i < MoreTabPages.count; i++) {
//Get the current table view controller page
UIWebView *webController= [MoreTabPages objectAtIndex:i];
//Request the URL and load the request
NSURL *urll =[NSURL URLWithString:[websites objectAtIndex:i]];
//Run requests in seperate thread
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
NSURLRequest *firstReq = [NSURLRequest requestWithURL:urll];
[webController loadRequest:firstReq];
dispatch_sync(dispatch_get_main_queue(), ^{
//Create a frame for the current table view controller
CGRect frame = webController.frame;
frame.origin.x = self.MoreTabScrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.MoreTabScrollView.frame.size;
webController.frame = frame;
//Add the the current table view controller page to the scroll view
[self.MoreTabScrollView addSubview:webController];
//Release the controller object it is no longer needed
[webController release];
if(i == 3){
[spinner stopAnimating];
}
});
});
}
}
//Set al the properties for the scroll view and page controll
-(void)setScrollandPageViewProperties{
self.MoreTabScrollView.contentSize = CGSizeMake(self.MoreTabScrollView.frame.size.width * MoreTabPages.count,
self.MoreTabScrollView.frame.size.height);
self.MoreTabScrollView.scrollsToTop = NO;
self.MoreTabScrollView.contentOffset = CGPointMake(self.MoreTabScrollView.frame.size.width, 0);
self.MoreTabPageControl.numberOfPages = MoreTabPages.count;
}
-(void)setupSpinner{
spinner.hidesWhenStopped = YES;
[spinner startAnimating];
}
//Called if the application receives a memory warning
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Called when the UIViewController's reference count goes to zero
- (void)dealloc {
[super dealloc];
[MoreTabPageControl release];
[MoreTabScrollView release];
[MoreTabTitle release];
[MoreTabPages release];
[titles release];
[websites release];
[spinner release];
}
#end
try setting the minimum zoom scale for your each webview.
[self.aboutWebView.scrollView setMinimumZoomScale:0.1]
it will throw the same error if the scrollview reaches zero at zero zoom.
This may happen with affine transformations when you're scaling UIScrollView instance to 0 either using setZoomScale:animated: method or zoomScale property, so please check your scroll views.
Make sure your zoomScale, minimumZoomScale and maximumZoomScale to set to at least 0.1.
Related:
Calculating minimumZoomScale of a UIScrollView
UIScrollView not respecting minimumZoomScale after changing the subview
I want to add activity indicator to a web view.
But i don't know when web view finish loading.
I start animating in viewdidload..
You shouldn't start animating in viewDidLoad. Conform to the
UIWebViewDelegate
protocol and make your web view's delegate your view controller, then use the delegate methods:
#interface MyVC: UIViewController <UIWebViewDelegate> {
UIWebView *webView;
UIActivityIndicatorView *activityIndicator;
}
#end
#implementation MyVC
- (id)init
{
self = [super init];
// ...
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame = CGRectMake(x, y, w, h);
[self.view addSubview:activityIndicator];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, w, h)];
webView.delegate = self;
// ...
return self;
}
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq
{
[activityIndicator startAnimating];
return YES;
}
- (void)webViewDidFinishLoading:(UIWebView *)wv
{
[activityIndicator stopAnimating];
}
- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error
{
[activityIndicator stopAnimating];
}
#end
Implement the UIWebViewDelegate protocol
These are the delegates you need to implement in your code:
- (void)webViewDidStartLoad:(UIWebView *)webView; //a web view starts loading
- (void)webViewDidFinishLoad:(UIWebView *)webView;//web view finishes loading
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; //web view failed to load
You will want to listen for the web view delegate callbacks to correctly show your activity indicator.
Specifically you will want to listen for:
webViewDidStartLoad: (start your activity indicator animation)
webViewDidFinishLoad: (end it)
webView:didFailLoadWithError: (end it)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webViewRef.delegate = self;
NSURL *websiteUrl = [NSURL URLWithString:Constants.API_TERMS_AND_CONDITIONS_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webViewRef loadRequest:urlRequest];
}
#pragma mark
#pragma mark -- UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
[self.activityIndicator startAnimating];
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
[self.activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
}