viewdidLoad/ viewDidAppear issue - iphone

I m loading a view which gets data from webservices. So depending upon the response code from server I show the current view or redirect to another view (ErrorView) if responsecode is 400.But I'm not being redirected to ErrorView and find a message in console "Attempt to present <ErrorView: 0xe332f30> on ViewController while a presentation is in progress!"
After googling it out I found to place vieDidLoad code to viewDidAppear
After placing the entire code , it redirects to error view and works correctly.But in ios7 for Status bar issue I used the code
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(responsecode==200)
{
//load current view
}
else
{
//Load anotherview(ErrorView)
}
float systemVersion=[[[UIDevice currentDevice] systemVersion] floatValue];
if(systemVersion>=7.0f)
{
CGRect tempRect;
for(UIView *sub in [[self view] subviews])
{
tempRect = [sub frame];
tempRect.origin.y += 20.0f;
[sub setFrame:tempRect];
}
}
}
First the statusbar appear on the top ,then after loading the entire viewdata , view gets adjusted and status bar dropsdown by 20px , which looks odd.Can I get it to be adjusted automatically before the viewloads completely ?
Any ideas/suggestions would be appreciable.

I think, you should seperate this code
-(void)viewDidLoad
{
float systemVersion=[[[UIDevice currentDevice] systemVersion] floatValue];
if(systemVersion>=7.0f)
{
CGRect tempRect;
for(UIView *sub in [[self view] subviews])
{
tempRect = [sub frame];
tempRect.origin.y += 20.0f;
[sub setFrame:tempRect];
}
}
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if(responsecode==200)
{
//load current view
}
else
{
//Load anotherview(ErrorView)
}
}

Related

How to complete interactive UIViewController transition?

I've been dabbling with the new iOS 7 custom transition API and looked through all the tutorials/documentation I could find but I can't seem to figure this stuff out for my specific scenario.
So essentially what I'm trying to implement is a UIPanGestureRecognizer on a view where I would swipe up and transition to a VC whose view would slide up from the bottom while the current view would slide up as I drag my finger higher.
I have no problem accomplishing this without the interaction transition, but once I implement the interaction (the pan gesture) I can't seem to complete the transition.
Here's the relevant code from the VC that conforms to the UIViewControllerTransitionDelegate which is needed to vend the animator controllers:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Swipe"]) {
NSLog(#"PREPARE FOR SEGUE METHOD CALLED");
UIViewController *toVC = segue.destinationViewController;
[interactionController wireToViewController:toVC];
toVC.transitioningDelegate = self;
toVC.modalPresentationStyle = UIModalPresentationCustom;
}
}
#pragma mark UIViewControllerTransition Delegate Methods
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController: (UIViewController *)presented presentingController: (UIViewController *)presenting sourceController:(UIViewController *)source {
NSLog(#"PRESENTING ANIMATION CONTROLLER CALLED");
SwipeDownPresentationAnimationController *transitionController = [SwipeDownPresentationAnimationController new];
return transitionController;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
NSLog(#"DISMISS ANIMATION CONTROLLER CALLED");
DismissAnimatorViewController *transitionController = [DismissAnimatorViewController new];
return transitionController;
}
- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator {
NSLog(#"Interaction controller for dimiss method caled");
return interactionController.interactionInProgress ? interactionController:nil;
}
NOTE: The interaction swipe is only for the dismissal of the VC which is why it's in the interactionControllerForDismissal method
Here's the code for the animator of the dismissal which works fine when I tap on a button to dismiss it:
#import "DismissAnimatorViewController.h"
#implementation DismissAnimatorViewController
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
return 1.0;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
NSTimeInterval duration = [self transitionDuration:transitionContext];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
CGRect initialFrameFromVC = [transitionContext initialFrameForViewController:fromVC];
UIView *containerView = [transitionContext containerView];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
NSLog(#"The screen bounds is :%#", NSStringFromCGRect(screenBounds));
toVC.view.frame = CGRectOffset(initialFrameFromVC, 0, screenBounds.size.height);
toVC.view.alpha = 0.2;
CGRect pushedPresentingFrame = CGRectOffset(initialFrameFromVC, 0, -screenBounds.size.height);
[containerView addSubview:toVC.view];
[UIView animateWithDuration:duration
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
fromVC.view.frame = pushedPresentingFrame;
fromVC.view.alpha = 0.2;
toVC.view.frame = initialFrameFromVC;
toVC.view.alpha = 1.0;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
#end
Here's the code for the UIPercentDrivenInteractiveTransition subclass which serves as the interaction controller:
#import "SwipeInteractionController.h"
#implementation SwipeInteractionController {
BOOL _shouldCompleteTransition;
UIViewController *_viewController;
}
- (void)wireToViewController:(UIViewController *)viewController {
_viewController = viewController;
[self prepareGestureRecognizerInView:_viewController.view];
}
- (void)prepareGestureRecognizerInView:(UIView*)view {
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
gesture.minimumNumberOfTouches = 1.0;
[view addGestureRecognizer:gesture];
}
- (CGFloat)completionSpeed {
return 1 - self.percentComplete;
NSLog(#"PERCENT COMPLETE:%f",self.percentComplete);
}
- (void)handleGesture:(UIPanGestureRecognizer*)gestureRecognizer {
// CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview];
CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview];
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
// 1. Start an interactive transition!
self.interactionInProgress = YES;
[_viewController dismissViewControllerAnimated:YES completion:nil];
break;
case UIGestureRecognizerStateChanged: {
// 2. compute the current position
CGFloat fraction = fabsf(translation.y / 568);
NSLog(#"Fraction is %f",fraction);
fraction = fminf(fraction, 1.0);
fraction = fmaxf(fraction, 0.0);
// 3. should we complete?
_shouldCompleteTransition = (fraction > 0.23);
// 4. update the animation controller
[self updateInteractiveTransition:fraction];
NSLog(#"Percent complete:%f",self.percentComplete);
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled: {
// 5. finish or cancel
NSLog(#"UI GESTURE RECOGNIZER STATE CANCELED");
self.interactionInProgress = NO;
if (!_shouldCompleteTransition || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
[self cancelInteractiveTransition];
NSLog(#"Interactive Transition is cancled.");
}
else {
NSLog(#"Interactive Transition is FINISHED");
[self finishInteractiveTransition];
}
break;
}
default:
NSLog(#"Default is being called");
break;
}
}
#end
Once again, when I run the code now and I don't swipe all the way to purposefully cancel the transition, I just get a flash and am presented with the view controller I want to swipe to. This happens regardless if the transition completes or is canceled.
However, when I dismiss via the button I get the transition specified in my animator view controller.
I can see a couple of issues here - although I cannot be certain that these will fix your problem!
Firstly, your animation controller's UIView animation completion block has the following:
[transitionContext completeTransition:YES];
Whereas it should return completion based on the result of the interaction controller as follows:
[transitionContext completeTransition:![transitionContext transitionWasCancelled]]
Also, I have found that if you tell the UIPercentDrivenInteractiveTransition that a transition is 100% complete, it does not call the animation controller completion block. As a workaround, I limit it to ~99.9%
https://github.com/ColinEberhardt/VCTransitionsLibrary/issues/4
I've created a number of example interaction and animation controllers here, that you might find useful:
https://github.com/ColinEberhardt/VCTransitionsLibrary
I had this same problem. I tried the fixes above and others, but nothing worked. Then I stumbled upon https://github.com/MrAlek/AWPercentDrivenInteractiveTransition, which fixed everything.
Once you add it to your project, just replace UIPercentDrivenInteractiveTransition with AWPercentDrivenInteractiveTransition.
Also, you have to set the animator before starting an interactive transition. In my case, I use the same class for UIViewControllerAnimatedTransitioning and UIViewControllerInteractiveTransitioning, so I just did it in init():
init() {
super.init()
self.animator = self
}

MkAnnotation drop animation iOS 6

I try to animate my custom MkAnnotation, it works great on iOS 5 but not on iOS 6. Here is my didAddAnnotationViews method :
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)annotationViews
{
NSTimeInterval delayInterval = 0;
for (MKAnnotationView *annView in annotationViews)
{
// Don't pin drop if annotation is user location
if ([annView.annotation isKindOfClass:[MKUserLocation class]]) {
continue;
}
// Check if current annotation is inside visible map rect, else go to next one
MKMapPoint point = MKMapPointForCoordinate(annView.annotation.coordinate);
if (!MKMapRectContainsPoint(self.mapView.visibleMapRect, point)) {
continue;
}
CGRect endFrame = annView.frame;
// Move annotation out of view
annView.frame = CGRectMake(annView.frame.origin.x, annView.frame.origin.y - self.view.frame.size.height, annView.frame.size.width, annView.frame.size.height);
[UIView animateWithDuration:1
delay:delayInterval
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
annView.frame = endFrame;
}
completion:^(BOOL finished){
if (isModal)
[self.mapView selectAnnotation:[[self.mapView annotations] objectAtIndex:0] animated:YES];
}];
delayInterval += 0.0625;
}
}
I made this method with some parts of code i found on internet. On iOS 5 the animation is perfect, but on iOS 6 pins are just appearing without any kind of animation. Setting the mapView delegate is the first thing I do on my viewDidLoad, and i've also tried to generate my annotations from viewDidAppear method , without success.
Any idea ?
Thanks.
EDIT : Solution found, I use the perform:withObject:afterDelay: method and it seems to work.
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
[self performSelector:#selector(addAnnotation) withObject:nil afterDelay:0.0];
}
- (void)addAnnotation
{
MapViewAnnotation *annotation = [[MapViewAnnotation alloc] initWithTitle:#"test" andCoordinate:CLLocationCoordinate2DMake(49.6, 6.2)];
[mapView addAnnotation:annotation];
}
I found a solution, look at the edit section.

How to reset an UIGestureRecognizer?

I have a subview inside a view. That view has a PanGestureRecognizer.
I want user could zoom in inside the webview, pan around, zoom out, etc.
But I want that, when the user reaches left edge of the webview, the gesturesrecognizers inside the webView to be ignored, and only the PanGestureRecognizer from the view get called.
Well, I have accomplished this, by that code:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float coordY = scrollView.contentOffset.y;
float correctionOffsetRightSide = scrollView.contentSize.width-webView.bounds.size.width;
if(scrollView.contentOffset.x <= 0)
{
[delegate viewOnlyRecognizeItsPan];
[scrollView setContentOffset:CGPointMake(0, coordY)];
}
else if (scrollView.contentOffset.x >= correctionOffsetRightSide)
{
[delegate viewOnlyRecognizeItsPan];
[scrollView setContentOffset:CGPointMake(correctionOffsetRightSide, coordY)];
}
}
-(void)viewOnlyRecognizeItsPan
{
for (UIGestureRecognizer *gesture in [[[[arrayDeAbas objectAtIndex:currentViewTag] webView] scrollView] gestureRecognizers])
{
[gesture requireGestureRecognizerToFail:panRecognizer];
}
}
"arrayDeAbas" is an mutableArray that stores webviews.
My problem is: after user touchs a button, I need to reset "gestures", so that it doesn't requires panRecognizer to fail anymore.
How could I could this?
EDITED:
Ok, I've founded a solution. Instead of using requireGestureRecognizerToFail:, I can simply setEnablle to YES or NO.
-(void)turnPanON
{
for (UIGestureRecognizer *gesture in [[[[arrayDeAbas objectAtIndex:currentViewTag] webView] scrollView] gestureRecognizers])
{
gesture.enabled = NO;
}
}
-(void)turnPanOFF;
{
for (UIGestureRecognizer *gesture in [[[[arrayDeAbas objectAtIndex:currentViewTag] webView] scrollView] gestureRecognizers])
{
gesture.enabled = YES;
}
}

Paged UIScrollview with lazy delayed loading

I need help with the uiscrollview implementation with the following requirements:
1) pagingEnabled = true.
2) lazy pages loading.
3) pages are loading in the background. So i need at first run loading the page X, then get the notification that the page is fully loaded and only then allow the user to scroll to it.
4) ability to change the current page.
My first attempt was to override the scrollViewDidEndDeacelerating and scrollViewDidScroll, but I had troubles with stucking on half of pages (when you stop the scroll on the half of the page and then wait for new page to add to the scroll) and empty pages (when the user scrolled too fast).
My second attempt was to override the layoutSubviews method of the UIScrollView and do all calculations there. But it seems to be very sofisticated.
So, I'd love to find any examples of similar implementations.
Now I have the code like this:
I've implemented scrollViewWillBeginDragging and scrollViewDidEndDecelerating:
- (void)scrollViewWillBeginDragging:(UIScrollView *)aScrollView
{
isScrolling = YES;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView
{
isScrolling = NO;
// Here we load the page that was prepared when the user was scrolling
if (needDisplay > -1) {
//NSLog(#"Loading queued page %d", needDisplay);
[self loadScrollViewWithPage:needDisplay forceAddToSuperview:NO animated:YES];
needDisplay = -1;
}
// minLoadedPageIndex - index of the first loaded page.
int selectedIndex = MIN(floor((aScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1 + minLoadedPageIndex, [photoItems count] - 1);
[self loadScrollViewWithPage:selectedIndex - 1 forceAddToSuperview:NO animated:YES];
[self loadScrollViewWithPage:selectedIndex forceAddToSuperview:NO animated:YES];
[self loadScrollViewWithPage:selectedIndex + 1 forceAddToSuperview:NO animated:YES];
}
In loadScrollViewWithPage I create the page view controller which loads the data from the server in the background. I don't add the view to the scrollview until it loads the data from the server.
- (void)loadScrollViewWithPage:(int)page forceAddToSuperview:(BOOL)value animated:(BOOL)animated
{
DetailController *controller = page >= viewControllers.count ? [NSNull null] :[viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[DetailController alloc] initWithNibName:#"DetailController" bundle:nil];
controller.delegate = self;
controller.view.hidden = NO; //this will call viewDidLoad.
if (page >= viewControllers.count) {
[viewControllers addObject:controller];
}
else {
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
[controller release];
}
// add the controller's view to the scroll view
if (controller.view && controller.view.superview == nil && (controller.isLoadedOrFailed || value)) {
[self setNumberOfVisiblePages:visiblePagesCount+1];
if (page < selectedIndex) {
// We are adding the page to the left of the current page,
// so we need to adjust the content offset.
CGFloat offset = (int)scrollView.contentOffset.x % (int)scrollView.frame.size.width;
offset = scrollView.frame.size.width * (selectedIndex - minLoadedPageIndex) + offset;
[scrollView setContentOffset:CGPointMake(offset, 0.0) animated:animated];
}
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * (page - minLoadedPageIndex);
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
[controller viewWillAppear:NO];
}
}
Also I have a detailControllerDidFinishDownload method which is called when data for the page view controller has been loaded.
- (void)detailControllerDidFinishDownload:(DetailController *)controller
{
... //here I calculate new minLoadedPageIndex value
// reset pages frames
if (minLoadedPageIndex < oldMinPage) {
for(int i=oldMinPage;i < [viewControllers count]; i++) {
DetailController *detailsController = [viewControllers objectAtIndex:i];
if ((NSNull *)detailsController != [NSNull null]) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * (i - minLoadedPageIndex);
frame.origin.y = 0;
[detailsController.view setFrame:frame];
}
}
}
// load the page now or delay load until the scrolling will be finished
if (!isScrolling) {
[self loadScrollViewWithPage:[photoItems indexOfObject:controller.photoItem] forceAddToSuperview:NO animated:NO];
}
else {
needDisplay = [photoItems indexOfObject:controller.photoItem];
//NSLog(#"pageControlUsed is used!!! %d", [photoItems indexOfObject:controller.photoItem]);
}
}
The problem I have now is that sometimes the scroll stucks on the middle (or somewhere near to middle) of the pages and it won't go to the nearest page bounce until I slightly more it. My tests showed that this situation happens if I scroll out of content view frame (the scroll view have bounces on) and wait for the new page to load. In 1 of the 10 times the scroll stucks.
Thanks, Mikhail!
There are a lot of things you are requiring at the same time. I suggest you have a look at this example.
http://ykyuen.wordpress.com/2010/05/22/iphone-uiscrollview-with-paging-example/
It's a very good starting point.

UIViewController and Orientation am I heading in the right direction?

I have spent the last several hours trying to get this to work and I can't so I need to know if what I am trying to do is the correct thing. Its driving me mad!
My goal is to have a ViewController that detects orientation change. When it's portrait it shows a view with a UITableView, when its landscape it shows a UIView with content that I will programmatically create.
In my parent viewcontroller I have:
- (void)viewDidLoad
{
[super viewDidLoad];
TableDataViewController *tableDataController = [[TableDataViewController alloc]
initWithNibName:#"TableDataViewController"
bundle:nil];
self.tableDataViewController = tableDataController;
[self.view insertSubview: tableDataController.view atIndex:0];
[tableDataController release];
}
This loads my view containing the table view and the controller that goes with it. The user then rotates the device and the function:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
duration:(NSTimeInterval)duration
{
if (toOrientation == UIInterfaceOrientationPortrait) {
NSLog(#"PerformAnalysisViewController: Gone to Potrait");
}
if ((toOrientation == UIInterfaceOrientationLandscapeLeft) ||
(toOrientation == UIInterfaceOrientationLandscapeRight)) {
NSLog(#"PerformAnalysisViewController: Gone to Landscape");
// Load new view here
CGRect frame = [[UIScreen mainScreen] applicationFrame];
UIView *horizView = [[[HorizView alloc]
initWithFrame:frame] autorelease];
[self setView:horizView];;
}
}
will trigger depending. However it doesn't trigger? Is this because control has passed to the Subview I have inserted in the viewDidLoad? If so how do I get it back? Do I have to get it to detect orientation and then remove itself from the superview?
If that was then working would the new view be added as I have it above? I have tried reading the Apple documentation but I can't make this work.
All help greatly appreciated.
Mike
I am using the willRotateToInterfaceOrientation method in order to handle UI rotation:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
if( interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
self.backgroundImageView.image = [UIImage imageNamed:#"vertical.png"];
}
else {
self.backgroundImageView.image = [UIImage imageNamed:#"horizontal.png"];
}
}
Please check, that your Info.plist supports more than one orientation and that you have implemented shouldAutorotateToInterfaceOrientation:interfaceOrientation by returning YES.