scrollView delegates not working (scrollViewDidEndDecelerating) - iphone

I am building a sort of slide show where the user slides through the images himself via a scrollView with paging enabled. i have a view controller for portrait and a view controller for landscape. The portrait view controller works fine with the "scrollViewDidEndDecelerating" function but i did the same exact thing on the landscape view controller and it does not respond.
- (void)viewDidLoad {
[super viewDidLoad];
imageNamesArray = [[NSMutableArray alloc] initWithCapacity:LNumImages];
int x = 0;
for(x=0; x<LNumImages;x++) {
[imageNamesArray insertObject: [NSString stringWithFormat:#"kr_Page_%d.png",x+1] atIndex:x];
}
LScrollView.delegate = self;
LScrollView.pagingEnabled = YES;
LScrollView.showsHorizontalScrollIndicator = YES;
LScrollView.contentSize = CGSizeMake(LScrollWidth * LNumImages, LScrollHeight);
[self initImages:0];
}
- (void)LScrollViewDidEndDecelerating:(UIScrollView *)LScrollView {
NSLog(#"stopped");
//does not get called
//[self arangeImages];
}

Because you're not implementing a UIScrollViewDelegate method. Note you're implementing LScrollViewDidEndDecelerating: and not scrollViewDidEndDecelerating:
See the UIScrollViewDelegate documentation for more information.

Related

UIImagePickerControllerCameraDeviceFront works every other time

This question is very similar to an existing question asked here UIImagePickerControllerCameraDeviceFront only works every other time I tried the solution presented but it didn't work for me
I have a simplest of a project with two view controllers. In the blue one I am displaying a small UIView with a UIImagePickerController in it. NOTE: I am displaying front facing camera when app is launched.
I hit the next button and go to orange view controller and when I hit the back button and come back to blue view controller the UIImagePickerController flips from Front to rear. I guess the reason is that it thinks its busy and moves to the rear cam. If I keep moving back and forth between the view controllers the camera keeps flipping front, back, front, back, front, back...
Here is my code and screenshots, what am I doing wrong?
In my *.h
#import <UIKit/UIKit.h>
#interface v1ViewController : UIViewController <UIImagePickerControllerDelegate>
{
UIImagePickerController *picpicker;
UIView *controllerView;
}
#property (nonatomic, retain) UIImagePickerController *picpicker;
#property (nonatomic, retain) UIView *controllerView;
#end
In my *.m file (This code is only used when blue colored view controller is displayed)
#import "v1ViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
#implementation v1ViewController
#synthesize picpicker;
#synthesize controllerView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
controllerView = picpicker.view;
[controllerView setFrame:CGRectMake(35, 31, 250, 250)];
controllerView.alpha = 0.0;
controllerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
[self.view addSubview:controllerView];
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
controllerView.alpha = 1.0;
}
completion:nil
];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[picpicker dismissModalViewControllerAnimated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
You are dismissing the controller in both the viewDidDisappear and viewWillDisappear methods.
That could be the cause of your problem.
Although I do not have a device with a camera available right now to verify this, it seems that you're not dismissing the pickerview controller correctly. The documentation states that you should call dismissModalViewControllerAnimated: on the parent controller in order to dismiss the picker (though, calls to presented controllers will propagate to presenters - so this is not the problem), but in your case you're not displaying the controller modally in the first place so it will not work.
What I would try in this case is to release the picker instead (if not under ARC) and set it to nil (instead of calling [picpicker dismissModalViewControllerAnimated:YES];).
PS. In fact, it seems that there is a bigger problem with your design. Since each button is set to present the other party modally you are not dismissing any of the controllers ever. The controllers just keep stacking on each other. You should either consider to embed them in a navigation controller and have it handle the hierarchy or just set dismissModalViewControllerAnimated: (dismissViewControllerAnimated:completion: on iOS5+) as the action of the second controller's button instead of a modal segue.
This is a very simple issue. I don't know why this happens exactly, but it seems that UIImagePickerController was designed to recreated each time it's needed instead of keeping any reference to it, which seems logical if you think about it. Basically, you need to recreate and reconfigure your picker each time. Below I've pasted some code to give an image of what I mean.
Simple solution:
- (UIImagePickerController *)loadImagePicker {
UIImagePickerController *picpicker = [[UIImagePickerController alloc] init];
picpicker.delegate = self;
picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picpicker.showsCameraControls = NO;
picpicker.navigationBarHidden = NO;
picpicker.wantsFullScreenLayout = NO;
return picpicker;
}
and in:
-(void)viewWillAppear:(BOOL)animated{
if(!self.picpicker){
self.picpicker = [self loadImagePicker];
[self.view addSubview: self.picpicker];
}
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.picpicker removeFromSuperview];
self.picpicker = nil;
}

Objective-C how to disable user interaction selectively

I have a Main View Controller that has many subviews. What I want is to disable all other views except one subview and its subviews programmatically from the subview file. But all I get is all frozen views. What did I do wrong?
I tried this code:
#define kDontDisableUserInteraction 321
- (id)initWithFrame:(CGRect)frame
{
NSLog(#"initWithFrame");
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.tag = kDontDisableUserInteraction;
}
return self;
}
-(void)something{
MVC *myController = [self getMVC];
for (UIView* subview in myController.view.subviews) {
NSLog(#"subview.tag %i", subview.tag);
if (subview.tag != kDontDisableUserInteraction){
subview.userInteractionEnabled = NO;
}
}
for (UIView *view in self.subviews){
NSLog(#"enabled!");
view.userInteractionEnabled = YES;
}
}
- (MVC *)getMVC {
Class vcc = [MVC class]; // Called here to avoid calling it iteratively unnecessarily.
UIResponder *responder = self;
while ((responder = [responder nextResponder])) if ([responder isKindOfClass: vcc]) return (MVC *)responder;
return nil;
}
Following links may be helpful:
How to disable touch input to all views except the top-most view?
UIView -- "user interaction enabled" false on parent but true on child?
I solved it by applying a full screen of a button on all other views and get the one view that I want to have user interaction upon the button. This way I disallow the user to click on any function except the one view I want the user to click on certain functions.

Trouble with datasource not being called from viewWillAppear

A little background. I have taken GCCalendar which only works in portrait orientation and extended it to work in landscape similar to how the iPhone's Calendar app works. I did this by duplicating the main view class and modifying it to work in landscape. So when the phone is in portrait orientation the CGCalendar is instantiated from the apps main view controller using the original portrait view class and when in landscape orientation using the new modified landscape view class. Most of the other classes in GCCalendar are shared without modification. A few had to be duplicated as well.
I got it all working great except for an issue with the datasource. The datasource is called when the calendar is first loaded and each time the user changes the dates being viewed. Problem is I can't get the datasource call to work on the first call.
I am stumped as it works fine in portrait orientation and I cannot find any difference between the 2 versions.
Following is some of the code that shows how it gets to the datasource call the first time. Subsequent calls removes all the calendar subviews and instantiates them again with the new dates. The duplicated landscape class names end in LS. Otherwise as you can see they are identical.
Does anyone has any idea of where else I might look to resolve this issue?
Thanks,
John
--------------------------------
//App main view controller
- (void)showLandscapeCalendar {
GCCalendarLandscapeView *calendar = [[[GCCalendarLandscapeView alloc] init] autorelease];
calendar.dataSource = self;
calendar.delegate = self;
navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
[self presentModalViewController:navigationController animated:YES];
}
- (void)showPortraitCalendar {
GCCalendarPortraitView *calendar = [[[GCCalendarPortraitView alloc] init] autorelease];
calendar.dataSource = self;
calendar.delegate = self;
navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
[self presentModalViewController:navigationController animated:YES];
}
- (NSArray *)calendarEventsForDate:(NSDate *)date{
//build and return the events array
//this is the protocol datasource method
//It is supposed to run every time the date changes in the calendar
}
-------------------------------
// GCCalendarLandscapeView...
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (viewDirty) {
[self reloadDayAnimated:NO context:NULL];
viewDirty = NO;
}
viewVisible = YES;
}
- (void)reloadDayAnimated:(BOOL)animated context:(void *)context {
GCCalendarDayViewLS *nextDayView = [[GCCalendarDayViewLS alloc] initWithCalendarView:self];
}
-------------------------------
//GCCalendarDayViewLS
- (id)initWithCalendarView:(GCCalendarView *)view {
if (self = [super init]) {
dataSource = view.dataSource;
}
return self;
}
- (void)reloadData {
//** first time through the dataSource method does not run
events = [dataSource calendarEventsForDate:date];
}
-------------------------------
// GCCalendarPortraitView...
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (viewDirty) {
[self reloadDayAnimated:NO context:NULL];
viewDirty = NO;
}
viewVisible = YES;
}
- (void)reloadDayAnimated:(BOOL)animated context:(void *)context {
GCCalendarDayView *nextDayView = [[GCCalendarDayView alloc] initWithCalendarView:self];
}
-------------------------------
//GCCalendarDayView
- (id)initWithCalendarView:(GCCalendarView *)view {
if (self = [super init]) {
dataSource = view.dataSource;
}
return self;
}
- (void)reloadData {
**//this one works every time
events = [dataSource calendarEventsForDate:date];
}

iOS switching view controllers depending on the device orientation

I'm developing an Augmented Reality application, everything worked properly till now that I need two different kind of visualization (AR and Map) depending on the device orientation. In particular the application should use the landscapeViewController when the device is in landscape mode while it should use another controller (named faceUpViewController ) when the device's orientation is "face up". I tried doing it with two simple view controllers and it works fine. The problem happens when the landscapeViewController uses the AR controller. The view is completely white and I don't understand why. Both the two controllers are "contained" by a Root View Controller. I'm doing everything by coding so without nib files. Here is the code:
RootViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
- (void)deviceOrientationDidChange:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
if (self.landscapeViewController.view.superview == nil) {
if (self.landscapeViewController == nil) {
LandscapeViewController *lvc = [[LandscapeViewController alloc] init];
self.landscapeViewController = lvc;
[lvc release];
}
[self.faceUpViewController.view removeFromSuperview];
[self.view addSubview:self.landscapeViewController.view];
}
}
if (orientation == UIDeviceOrientationFaceUp) {
if (self.faceUpViewController.view.superview == nil) {
if (self.faceUpViewController == nil) {
FaceUpViewController *fvc = [[FaceUpViewController alloc] init];
self.faceUpViewController = fvc;
[fvc release];
}
[self.landscapeViewController.view removeFromSuperview];
[self.view addSubview:self.faceUpViewController.view];
}
}
}
#end
LandscapeViewController.m
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
UIView *landscapeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
landscapeView.backgroundColor = [UIColor yellowColor];
self.view = landscapeView;
[landscapeView release];
ARController *arC = [[ARController alloc] initWithViewController:self];
arC.landscapeViewController = self;
self.arController = arC;
[arC release];
}
//When the view appear present the camera feed
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_arController presentModalARControllerAnimated:NO];
}
FaceUpViewController.m
- (void)loadView
{
UIView *faceUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
faceUpView.backgroundColor = [UIColor blueColor];
self.view = faceUpView;
[faceUpView release];
}
ARController.m Very simple version
- (id) initWithViewController:(UIViewController *)theView{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.rootController = theView;
//Retrieve screen bounds
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIView *overlaidView = [[UIView alloc] initWithFrame: screenBounds];
self.overlayView = overlaidView;
[overlaidView release];
self.rootController.view = overlayView;
// Initialise the UIImagePickerController
UIImagePickerController *picker= [[UIImagePickerController alloc] init];
self.pickerController = picker;
[picker release];
self.pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.pickerController.cameraViewTransform = CGAffineTransformScale(
self.pickerController.cameraViewTransform, 1.0f, 1.12412f);
self.pickerController.showsCameraControls = NO;
self.pickerController.navigationBarHidden = YES;
self.pickerController.cameraOverlayView = _overlayView;
}
return self;
}
- (void)presentModalARControllerAnimated:(BOOL)animated{
[self.rootController presentModalViewController:[self pickerController] animated:animated];
self.overlayView.frame = self.pickerController.view.bounds;
}
#end
I say again that I'm doing everything by coding thereby without nib files.
I really appreciate any advice!
Thanks
The primary problem with adding and removing your "child" view controllers' views as you've done here is that the view controller life cycle methods (viewWillAppear:, viewDidAppear:, etc.) won't ever get called on your child view controllers. Containers like UINavigationController and UITabBarController have always known how to delegate methods like these appropriately to their children, but UIViewController didn't officially support the ability to nest view controllers under your own custom container before iOS 5. It was possible, but it took a lot more work to do it right.
If you want to stick with the approach of adding and removing subviews, you have two options:
Require iOS 5+, and call addChildViewController:, removeFromParentViewController,
transitionFromViewController:toViewController:duration:options:animations:completion:,
willMoveToParentViewController:, and
didMoveToParentViewController: as described in the Implementing a Container View Controller section of the UIViewController Class Reference.
To support older iOS versions, you'll have to override many of the methods of the UIViewController class and delegate those calls manually to your child view controllers to make them behave as expected. I'd pay particular attention to the sections titled, "Responding to View Events", and "Responding to View Rotation Events" in the UIViewController Class Reference.
A different approach for pre-iOS 5 support is to present your child view controllers using presentModalViewController:animated: rather than adding their views as subviews to a container. Apple describes this approach in the View Controller Programming Guide for iOS under the section, Creating an Alternate Landscape Interface. The advantage of this approach is that your child view controllers are officially supported as first-class members of the view controller hierarchy, so UIKit will automatically manage their life cycles appropriately. You won't have to override and delegate all those methods manually.
You might want to try getting your acceptance rate up a little bit - more people would be willing to help you.
Anyway, wild guess: in your root controller, try putting the contents of
deviceOrientationDidChange
into
deviceOrientationWillChange.

UISplitView - load different detailView's for each row in masterView

I'm using UISplitViewController for app on iPad. The first task was to show master and detail view in portrait mode. I have done this like that:
// It is possible to keep the Master View in portrait mode
// also. Just pass YES to this method to enable this mode.
- (id) initWithMasterInPortraitMode:(BOOL) masterInPortrait {
self = [super init];
self.keepMasterInPortraitMode = masterInPortrait;
return self;
}
// Thanks to http://intensedebate.com/profiles/fgrios for this code snippet
-(void) viewWillAppear:(BOOL)animated {
NSLog(#"viewWillAppear");
if(keepMasterInPortraitMode == NO) {
return;
}
//check interface orientation at first view and adjust it
//if it is in portrait mode
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
UIViewController* master = [self.viewControllers objectAtIndex:0];
UIViewController* detail = [self.viewControllers objectAtIndex:1];
[self setupPortraitMode:master detail:detail];
}
}
// Thanks to http://intensedebate.com/profiles/fgrios for this code snippet
- (void)setupPortraitMode:(UIViewController*)master detail:(UIViewController*)detail {
//adjust master view
CGRect f = master.view.frame;
f.size.width = 320;
f.size.height = 1024;
f.origin.x = 0;
f.origin.y = 0;
[master.view setFrame:f];
//adjust detail view
f = detail.view.frame;
f.size.width = 448;
f.size.height = 1024;
f.origin.x = 321;
f.origin.y = 0;
[detail.view setFrame:f];
}
I create splitView like this:
MySplitViewController *mySplitViewController = [[MySplitViewController alloc] initWithMasterInPortraitMode:YES];
Ok, that works ok and when I launch app in portrait mode it shows both master and detail view side by side.
Everything works for now. But I want to show a different view in detailView for each record (row) in master view.
My didSelectRowAtIndexPath method in masterView looks like that:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
FirstDetailViewController *firstDetailView = [[FirstDetailViewController alloc] initWithNibName:#"firstDetailView" bundle:nil];
UINavigationController *firstDetailNavigationController = [[UINavigationController alloc] initWithRootViewController:firstDetailView];
[firstDetailView release];
[self.splitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, firstDetailNavigationController, nil]];
}
else {
SecondDetailViewController *secondDetailView = [[SecondDetailViewController alloc] initWithNibName:#"secondDetailView" bundle:nil];
UINavigationController *secondDetailNavigationController = [[UINavigationController alloc] initWithRootViewController:secondDetailView];
[secondDetailView release];
[self.splitViewController setViewControllers:[NSArray arrayWithObjects:self.navigationController, secondDetailNavigationController, nil]];
}
}
After a click (touch) on first or second row (in masterView) splitView shows only detailView (whole screen) with no masterView.
How can I force splitView to show on change both views, master and detail view ???
Thanks for your help!
I have found the solution. In masterView I created splitViewController instance and assign local instance to that instance:
settingsSplitViewController = (SettingsSplitViewController *)self.splitViewController;
In method didSelectRowAtIndexPath I did this like that:
[settingsSplitViewController setupPortraitMode:self.navigationController detail:detailNavigationController];
Hope it will help someone else to solve this kind of problem.