The right 1/3 of a button created using a SKSpriteNode is not detecting touches - objective-c

In my game, I am creating numerous buttons. I am finding that many of the buttons are not detecting when a user touches down/up on the RIGHT side of the button. In other words, the button responds correctly when touched on the LEFT side of the button, but not the RIGHT side. In short, if you sliced the button into three parts, the right 1/3 of the button does not respond while the other 2/3 of the button work perfectly. Below is the code that I have used to implement my buttons. Can anyone tell me what I might be doing wrong? I did notice when I first started using Sprite Kit that my buttons would not respond at all when the SKSpriteNode::zPosition property was NOT set to 1 or greater...once I set this property my buttons started working. This zPosition problem occurred whether or not the button sprite node was the only sprite node in the scene or not which makes me think that the problem is not an overlap issue either. Got any ideas?
I have extended the SKSpriteNode class to have an additional method GESpriteNode::setGestureHandler:withKey:, which have the following signatures:
- (void) setGestureHandler: (void (^)(SKNode *node, GEGestureRecognizer *recognizer))handler withKey: (NSString *)key;
- (void) removeGestureHandlerWithKey: (NSString *)key;
And, implemented like so (where _event is an NSMutableDictionary):
- (void) setGestureHandler: (void (^)(SKNode *node, GEGestureRecognizer *recognizer))handler withKey: (NSString *)key {
if (key != nil) {
[_events setObject: [handler copy] forKey: key];
}
}
- (void) removeGestureHandlerWithKey: (NSString *)key {
if (key != nil) {
[_events removeObjectForKey: key];
}
}
- (void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event {
if (![self isHidden]) {
void (^handler)(SKNode *, GEGestureRecognizer *);
handler = [_events objectForKey: GETouchDownGestureHandlerKey];
if (handler != nil) {
GEGestureRecognizer *recognizer = [[GEGestureRecognizer alloc] initWithScene: [self scene] withTouches: touches];
if (recognizer != nil) {
handler(self, recognizer);
}
}
}
}
- (void) touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
if (![self isHidden]) {
void (^handler)(SKNode *, GEGestureRecognizer *);
handler = [_events objectForKey: GETouchUpGestureHandlerKey];
if (handler != nil) {
GEGestureRecognizer *recognizer = [[GEGestureRecognizer alloc] initWithScene: [self scene] withTouches: touches];
if (recognizer != nil) {
handler(self, recognizer);
}
}
}
}
They are used as follows:
GESpriteNode *spriteNode = [GESpriteNode spriteNodeWithImageNamed: NODE_BUTTON_1];
[spriteNode setAlpha: ALPHA_OPAQUE];
[spriteNode setGestureHandler: ^(SKNode *node, GEGestureRecognizer *recognizer) {
[node setScale: SCALE_95];
} withKey: GETouchDownGestureHandlerKey];
[spriteNode setGestureHandler: ^(SKNode *node, GEGestureRecognizer *recognizer) {
[node setScale: SCALE_100];
} withKey: GETouchUpGestureHandlerKey];
[spriteNode setHidden: NO];
[spriteNode setName: NODE_BUTTON_1];
[spriteNode setPosition: CGPointMake(384.0f, 387.0f)];
[spriteNode setScale: SCALE_100];
[spriteNode setUserInteractionEnabled: YES];
[spriteNode setZPosition: 1];
[self addChild: spriteNode];
My GEGestureRecognizer.h class is defined as so:
#import <SpriteKit/SpriteKit.h>
extern NSString * const GETouchDownGestureHandlerKey;
extern NSString * const GETouchUpGestureHandlerKey;
#interface GEGestureRecognizer : NSObject
// public properties
#property (nonatomic, strong) SKScene *scene;
// public methods
- (instancetype) initWithScene: (SKScene *)scene withTouches: (NSSet *)touches;
- (CGPoint) locationInScene: (SKScene *)scene;
#end
And, its GEGestureRecognizer.m is defined as:
#import "GEGestureRecognizer.h"
#pragma mark -
#pragma mark Constants
NSString * const GETouchDownGestureHandlerKey = #"GETouchDownGestureHandlerKey";
NSString * const GETouchUpGestureHandlerKey = #"GETouchUpGestureHandlerKey";
#interface GEGestureRecognizer () {
#private
SKScene *_scene;
NSSet *_touches;
}
#end
#implementation GEGestureRecognizer
#synthesize scene = _scene;
- (instancetype) initWithScene: (SKScene *)scene withTouches: (NSSet *)touches {
if ((self = [super init])) {
_scene = scene;
_touches = touches;
}
return self;
}
- (CGPoint) locationInScene: (SKScene *)scene {
CGFloat x = 0;
CGFloat y = 0;
CGFloat k = 0;
for (UITouch *touch in _touches) {
const CGPoint point = [touch locationInNode: scene];
x += point.x;
y += point.y;
k++;
}
if (k > 0) {
return CGPointMake(x/k, y/k);
}
return CGPointZero;
}
#end

What exactly is this code doing? Why do you increase x and y values? Why do you need k value? I think you are offsetting the location of touch, try logging all touch locations and drawing them on screen.
There are native methods to detect touch in nodes and translate coordinates to scene locations.

Recreating the GestureRecognizer on each event seems rather inefficient, the locationInScene does not make much sense either when multiple touches occur (no idea what you're trying to achieve there)
That said, if your buttons only started to work when you change the zPosition, I assume you do have an overlap issue.
Since there are other nodes involved (whether visible or not) at (default) zPosition 0, you'd have to show more code to get help.

Related

NSPathControl with popups for each component of the path?

From Apple's sample code and reading the docs I can see no way configuring the NSPathControl to behave similarly to e.g. the 'jump bar' in the Xcode Editor window:
I.e. have it represent a path (or other kind of hierarchy) and make each component of the path a clickable popup to navigate the hierarchy..?
Anybody having luck faking such behaviour using a NSPathControlDelegate listening to clicks and showing a menu in a temporary window?
Seems like a common design where one would even expect some OSS implementation - but no such luck yet googling for it..
I made a subclass of NSPathControl so that I can use mouseDown: to popup the context menus of the component cells at the right position. I added also a delegate to the menu to create deeper menus on demand.
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSPathCell *cell = self.cell;
NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point
withFrame:self.bounds
inView:self];
NSRect componentRect = [cell rectOfPathComponentCell:componentCell
withFrame:self.bounds
inView:self];
NSMenu *menu = [componentCell menuForEvent:event
inRect:componentRect
ofView:self];
if (menu.numberOfItems > 0) {
NSUInteger selectedMenuItemIndex = 0;
for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) {
if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) {
selectedMenuItemIndex = menuItemIndex;
break;
}
}
NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex];
[menu popUpMenuPositioningItem:selectedMenuItem
atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2)
inView:self];
}
}
- (NSMenu *)menuForEvent:(NSEvent *)event {
if (event.type != NSLeftMouseDown) {
return nil;
}
return [super menuForEvent:event];
}
I extended Stephan's answer slightly to accommodate for lazily loading the menu items. I created a small protocol to call for the menu rather than having to build the menu's ahead of time for each cell:
NSPathControlExtended.h
#protocol NSPathControlExtendedDelegate <NSPathControlDelegate>
#required
- (NSMenu *)pathControl:(NSPathControl *)pathControl menuForCell:(NSPathComponentCell *)cell;
#end
#interface NSPathControlExtended : NSPathControl
#property (weak) id <NSPathControlExtendedDelegate> delegate;
#end
NSPathControlExtended.m
#import "NSPathControlExtended.h"
#implementation NSPathControlExtended
#synthesize delegate;
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSPathCell *cell = self.cell;
NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point
withFrame:self.bounds
inView:self];
NSRect componentRect = [cell rectOfPathComponentCell:componentCell
withFrame:self.bounds
inView:self];
NSMenu *menu = [delegate pathControl:self menuForCell:componentCell];
if (menu.numberOfItems > 0) {
NSUInteger selectedMenuItemIndex = 0;
for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) {
if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) {
selectedMenuItemIndex = menuItemIndex;
break;
}
}
NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex];
[menu popUpMenuPositioningItem:selectedMenuItem
atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2)
inView:self];
}
}
- (NSMenu *)menuForEvent:(NSEvent *)event {
if (event.type != NSLeftMouseDown) {
return nil;
}
return [super menuForEvent:event];
}
#end

tapAtPoint on UIWebView subclass

I have subclassed UIWebView so that I can get touch events and also implemented this handy method. I'm curious, if this will work on an actual iOS device. I'm not at the office, so I don't know if does. It seems to work in the simulator.
- (void) tapAtPoint:(CGPoint)point
{
id /*UIWebBrowserView*/ webBrowserView = nil;
id webViewInternal = nil;
object_getInstanceVariable(self, "_internal", (void **)&webViewInternal);
object_getInstanceVariable(webViewInternal, "browserView", (void **)&webBrowserView);
if (webBrowserView) {
[webBrowserView tapInteractionWithLocation:point];
}
}
Has anyone tried something like this? I for sure find out in the morning, lol.
Please try this code, Here its working fine.
/* TapDetectingWindow.m */
#import "TapDetectingWindow.h"
#implementation TapDetectingWindow
#synthesize viewToObserve;
#synthesize controllerThatObserves;
- (id)initWithViewToObserver:(UIView *)view andDelegate:(id)delegate {
if(self == [super init]) {
self.viewToObserve = view;
self.controllerThatObserves = delegate;
}
return self;
}
- (void)dealloc {
[viewToObserve release];
[super dealloc];
}
- (void)forwardTap:(id)touch {
[controllerThatObserves userDidTapWebView:touch];
}
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
if (viewToObserve == nil || controllerThatObserves == nil)
return;
NSSet *touches = [event allTouches];
if (touches.count != 1)
return;
UITouch *touch = touches.anyObject;
if (touch.phase != UITouchPhaseEnded)
return;
if ([touch.view isDescendantOfView:viewToObserve] == NO)
return;
CGPoint tapPoint = [touch locationInView:viewToObserve];
NSLog(#"TapPoint = %f, %f", tapPoint.x, tapPoint.y);
NSArray *pointArray = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%f", tapPoint.x],
[NSString stringWithFormat:#"%f", tapPoint.y], nil];
if (touch.tapCount == 1) {
[self performSelector:#selector(forwardTapwithObject:pointArray afterDelay:0.5];
}
else if (touch.tapCount > 1) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(forwardTap  object:pointArray];
}
}
#end
/* WebViewController.h */
#interface WebViewController : UIViewController<TapDetectingWindowDelegate> {
IBOutlet UIWebView *mHtmlViewer;
TapDetectingWindow *mWindow;
}
/* WebViewController.m */
- (void)viewDidLoad {
[super viewDidLoad];
mWindow = (TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0];
mWindow.viewToObserve = mHtmlViewer;
mWindow.controllerThatObserves = self;
}
- (void)userDidTapWebView:(id)tapPoint
{
NSLog(#"TapPoint = %f, %f", tapPoint.x, tapPoint.y);
}
Thanks, Let me know if you face any problems.
short answer: Yes, I tried something like this in the same way and it works on the real devices as well (tested with iOS 6).
ARC version of your method:
- (void) tapAtPoint:(CGPoint)point
{
Ivar internalWebViewIvar = class_getInstanceVariable([self class], "_internal");
id internalWebView = object_getIvar(self, internalWebViewIvar);
Ivar browserViewIvar = class_getInstanceVariable(object_getClass(internalWebView), "browserView");
id browserView = object_getIvar(internalWebView, browserViewIvar);
if (browserView) {
[browserView performSelector:#selector(tapInteractionWithLocation:) withObject:[NSValue valueWithCGPoint:point]];
}
}

Array of buttons returning null

I am trying to add the buttons I create to an array and then remove them buttons from the array. My array keeps returning null so I get the feeling my buttons are not even being added to my array?
I am a beginner. I am using Xcode 4.3. Here is my code:
//
// MainViewController.h
// Test-Wards
//
// Created by Dayle Pearson on 5/12/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
{
/*This stuff creates a timer */
IBOutlet UILabel *opponentsBlue;
NSTimer *timer;
int redBlue;
/*Stuff for making a label creator */
CGPoint startPoint;
int xStuff, yStuff;
/*array for storing wards*/
NSMutableArray *wardArray;
}
#property CGPoint startPoint;
- (IBAction)startRedBlue:(id)sender;
- (IBAction)removeWard:(id)
sender;
- (void)countdown;
#end
//
// MainViewController.m
// Test-Wards
//
// Created by Dayle Pearson on 5/12/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize startPoint;
- (void)countdown
{
if (redBlue < 2) {
[timer invalidate];
timer = nil;
}
redBlue -= 1;
opponentsBlue.text = [NSString stringWithFormat:#"%i", redBlue];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *theTouch = [touches anyObject];
startPoint = [theTouch locationInView:self.view];
}
- (IBAction)startRedBlue:(id)sender
{
UIButton *wardButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
wardButton.frame = CGRectMake((startPoint.x - 5), (startPoint.y - 5), 10, 10);
[wardButton setTitle:#"180" forState:UIControlStateNormal];
//add targets and actions
/*[wardButton addTarget:self action:#selector() forControlEvents:<#(UIControlEvents)#>*/
//add to a view
[self.view addSubview:wardButton];
[self->wardArray addObject: wardButton];
NSLog(#"This elemnt = %#", wardArray);
}
- (IBAction)removeWard:(id)sender
{
[self->wardArray removeLastObject];
}
- (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.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
}
}
#end
You forgot to initialize your wardArray. You should add
wardArray = [NSMutableArray array];
to your designated initializer.
In Objective-C sending messages to nil objects is legal - these messages are simply ignored. That's why you do not see the items that you added.
I also noticed that you add buttons to the view, but you never remove them. To remove the buttons from the screen, change the code as follows:
- (IBAction)removeWard:(id)sender
{
[[self->wardArray lastObject] removeFromSuperview];
[self->wardArray removeLastObject];
}
You have to initialize your array before you can add to it
wardArray = [NSMutableArray array]; // quick and easy
wardArray = [[NSMutableArray alloc] init]; // oldschool
I recommend doing it like so in your method. This will only initialize it once if it doesn't exist so theres no chance of it never being ready to have objects :)
- (IBAction)startRedBlue:(id)sender
{
....
// If wardArray doesn't exist we create it. Otherwise we add our ward to it.
if (!wardArray) {
wardArray = [NSMutableArray array];
} else {
[self->wardArray addObject: wardButton];
}
}
- (IBAction)removeWard:(id)sender
{
UIButton *ward = (UIButton *)sender;
[ward removeFromSuperview]; // Takes it off the screen.
[self->wardArray removeObject:ward]; //Takes it out of the array
}

NSInvocation invocationWithMethodSignature, method signature argument cannot be nil

I followed tutorial from cocos2d official site . I try to create some items for a menu when creating them i pass a selector with one parameter. For each item i pass different selector . I think here is the problem , but i dont see realy why here is the problem. My header file looks :
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import "CCTouchDispatcher.h"
// HelloWorldLayer
#interface HelloWorldLayer : CCLayer {
CCSprite *first;
CCSprite *second;
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
- (void) setUpMenus;
- (void) doSomethingOne: (CCMenuItem *) menuItem;
- (void) doSomethingTwo: (CCMenuItem *) menuItem;
- (void) doSomethingThree: (CCMenuItem *) menuItem;
#end
Implementation file :
// Import the interfaces
#import "HelloWorldLayer.h"
// HelloWorldLayer implementation
#implementation HelloWorldLayer
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
- (void) doSomethingOne: (CCMenuItem *) menuItem
{
NSLog(#"The first menu was called");
}
- (void) doSomethingTwo: (CCMenuItem *) menuItem
{
NSLog(#"The second menu was called");
}
- (void) doSomethingThree: (CCMenuItem *) menuItem
{
NSLog(#"The third menu was called");
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
first = [CCSprite spriteWithFile:#"seeker.png"];
first.position = ccp(100, 100);
[self addChild:first];
second = [CCSprite spriteWithFile:#"Icon.png"];
second.position = ccp(50, 50);
[self addChild:second];
[self schedule:#selector(nextFrame:)];
[self setUpMenus];
self.isTouchEnabled = YES;
}
return self;
}
- (void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [self convertTouchToNodeSpace: touch];
[second stopAllActions];
[second runAction: [CCMoveTo actionWithDuration:1 position:location]];
}
- (void) nextFrame:(ccTime)dt {
first.position = ccp( first.position.x + 100*dt, first.position.y );
if (first.position.x > 480+32) {
first.position = ccp( -32, first.position.y );
}
}
- (void) setUpMenus {
CCMenuItemImage *menuItem1 = [CCMenuItemImage itemFromNormalImage:#"myfirstbutton.png"
selectedImage:#"myfirstbutton_selected.png"
target:self
selector:#selector(doSomenthingOne:)];
CCMenuItemImage *menuItem2 = [CCMenuItemImage itemFromNormalImage:#"mysecondbutton.png"
selectedImage:#"mysecondbutton_selected.png"
target:self
selector:#selector(doSomenthingTwo:)];
CCMenuItemImage *menuItem3 = [CCMenuItemImage itemFromNormalImage:#"mythirdbutton.png"
selectedImage:#"mythirdbutton_selected.png"
target:self selector:#selector(doSomenthingThree:)];
CCMenu *myMenu = [CCMenu menuWithItems:menuItem1,menuItem2,menuItem3, nil];
[myMenu alignItemsVertically];
[self addChild:myMenu];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
#end
You've got the same typo in all three menu item creation calls. You're telling the menu items that the selector they should use is called doSomenthing... (note the spurious n in the middle):
CCMenuItemImage *menuItem1 = [... selector:#selector(doSomenthingOne:)];
CCMenuItemImage *menuItem2 = [... selector:#selector(doSomenthingTwo:)];
CCMenuItemImage *menuItem3 = [... selector:#selector(doSomenthingThree:)];
but the actual names of your methods are doSomethingOne:, doSomethingTwo:, and doSomethingThree:.
The exact cause of the error message is that later, when the menu item needs to perform that selector, it will ask your class to tell it the method signature for the selector you gave it. Since you gave the item an incorrect selector, your class doesn't know the signature, and it returns nil. The menu item tries to construct an NSInvocation object anyways to perform its action, which fails because the invocation can't be created with a nil signature.
Fix the typos and everything should work fine.

How do I use UIPageViewController to load separate XIBs?

I'm delving into the new world of UIPageViewControllers and there are a lot of tutorials out there, however all of them seem to create one view, and then just use new instances of it with different content.
I'd really like to be able to create multiple XIBs and then just chain them together with the UIPageViewController but it's too new and I can't get my head around the way it works.
Well, here's a long answer that you should be able to copy and paste. (This code was adapted from Erica Sadun (https://github.com/erica/iOS-5-Cookbook))
First, create a new class of type UIPageViewController. Call it BookController. Now paste the following code in your .h file.
// Used for storing the most recent book page used
#define DEFAULTS_BOOKPAGE #"BookControllerMostRecentPage"
#protocol BookControllerDelegate <NSObject>
- (id) viewControllerForPage: (int) pageNumber;
#optional
- (void) bookControllerDidTurnToPage: (NSNumber *) pageNumber;
#end
#interface BookController : UIPageViewController <UIPageViewControllerDelegate, UIPageViewControllerDataSource>
+ (id) bookWithDelegate: (id) theDelegate;
+ (id) rotatableViewController;
- (void) moveToPage: (uint) requestedPage;
- (int) currentPage;
#property (assign) id <BookControllerDelegate> bookDelegate;
#property (nonatomic, assign) uint pageNumber;
and in your .m file:
#define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define SAFE_ADD(_Array_, _Object_) {if (_Object_ && [_Array_ isKindOfClass:[NSMutableArray class]]) [pageControllers addObject:_Object_];}
#define SAFE_PERFORM_WITH_ARG(THE_OBJECT, THE_SELECTOR, THE_ARG) (([THE_OBJECT respondsToSelector:THE_SELECTOR]) ? [THE_OBJECT performSelector:THE_SELECTOR withObject:THE_ARG] : nil)
#pragma Utility Class - VC that Rotates
#interface RotatableVC : UIViewController
#end
#implementation RotatableVC
- (void) loadView
{
[super loadView];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor whiteColor];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
#end
#pragma Book Controller
#implementation BookController
#synthesize bookDelegate, pageNumber;
#pragma mark Debug / Utility
- (int) currentPage
{
int pageCheck = ((UIViewController *)[self.viewControllers objectAtIndex:0]).view.tag;
return pageCheck;
}
#pragma mark Page Handling
// Update if you'd rather use some other decision style
- (BOOL) useSideBySide: (UIInterfaceOrientation) orientation
{
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
return isLandscape;
}
// Store the new page and update the delegate
- (void) updatePageTo: (uint) newPageNumber
{
pageNumber = newPageNumber;
[[NSUserDefaults standardUserDefaults] setInteger:pageNumber forKey:DEFAULTS_BOOKPAGE];
[[NSUserDefaults standardUserDefaults] synchronize];
SAFE_PERFORM_WITH_ARG(bookDelegate, #selector(bookControllerDidTurnToPage:), [NSNumber numberWithInt:pageNumber]);
}
// Request controller from delegate
- (UIViewController *) controllerAtPage: (int) aPageNumber
{
if (bookDelegate &&
[bookDelegate respondsToSelector:#selector(viewControllerForPage:)])
{
UIViewController *controller = [bookDelegate viewControllerForPage:aPageNumber];
controller.view.tag = aPageNumber;
return controller;
}
return nil;
}
// Update interface to the given page
- (void) fetchControllersForPage: (uint) requestedPage orientation: (UIInterfaceOrientation) orientation
{
BOOL sideBySide = [self useSideBySide:orientation];
int numberOfPagesNeeded = sideBySide ? 2 : 1;
int currentCount = self.viewControllers.count;
uint leftPage = requestedPage;
if (sideBySide && (leftPage % 2)) leftPage--;
// Only check against current page when count is appropriate
if (currentCount && (currentCount == numberOfPagesNeeded))
{
if (pageNumber == requestedPage) return;
if (pageNumber == leftPage) return;
}
// Decide the prevailing direction by checking the new page against the old
UIPageViewControllerNavigationDirection direction = (requestedPage > pageNumber) ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
[self updatePageTo:requestedPage];
// Update the controllers
NSMutableArray *pageControllers = [NSMutableArray array];
SAFE_ADD(pageControllers, [self controllerAtPage:leftPage]);
if (sideBySide)
SAFE_ADD(pageControllers, [self controllerAtPage:leftPage + 1]);
[self setViewControllers:pageControllers direction: direction animated:YES completion:nil];
}
// Entry point for external move request
- (void) moveToPage: (uint) requestedPage
{
[self fetchControllersForPage:requestedPage orientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
}
#pragma mark Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
[self updatePageTo:pageNumber + 1];
return [self controllerAtPage:(viewController.view.tag + 1)];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
[self updatePageTo:pageNumber - 1];
return [self controllerAtPage:(viewController.view.tag - 1)];
}
#pragma mark Delegate
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
NSUInteger indexOfCurrentViewController = 0;
if (self.viewControllers.count)
indexOfCurrentViewController = ((UIViewController *)[self.viewControllers objectAtIndex:0]).view.tag;
[self fetchControllersForPage:indexOfCurrentViewController orientation:orientation];
BOOL sideBySide = [self useSideBySide:orientation];
self.doubleSided = sideBySide;
UIPageViewControllerSpineLocation spineLocation = sideBySide ? UIPageViewControllerSpineLocationMid : UIPageViewControllerSpineLocationMin;
return spineLocation;
}
-(void)dealloc{
self.bookDelegate = nil;
}
#pragma mark Class utility routines
// Return a UIViewController that knows how to rotate
+ (id) rotatableViewController
{
UIViewController *vc = [[RotatableVC alloc] init];
return vc;
}
// Return a new book
+ (id) bookWithDelegate: (id) theDelegate
{
BookController *bc = [[BookController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
bc.dataSource = bc;
bc.delegate = bc;
bc.bookDelegate = theDelegate;
return bc;
}
This Class can now be used to control any book you create in any project, and for multiple books in a single project. For each book, create a delegate UIPageViewController with the #interface:
#interface NameOfBookController : UIPageViewController <BookControllerDelegate>
In the .m file of this delegate, include:
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
[super loadView];
CGRect appRect = [[UIScreen mainScreen] applicationFrame];
self.view = [[UIView alloc] initWithFrame: appRect];
self.view.backgroundColor = [UIColor whiteColor];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// Establish the page view controller
bookController = [BookController bookWithDelegate:self];
bookController.view.frame = (CGRect){.size = appRect.size};
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
// Add the child controller, and set it to the first page
[self.view addSubview:bookController.view];
[self addChildViewController:bookController];
[bookController didMoveToParentViewController:self];
}
Then add:
- (id) viewControllerForPage: (int) pageNumber
{
// Establish a new controller
UIViewController *controller;
switch (pageNumber) {
case 0:
view1 = [[FirstViewController alloc] init];
view1.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
controller = view1;
//rinse and repeat with each new view controller
break;
case 1:
//etc.
break;
default:
return nil;
break;
}
return controller;
}
For the record, this code is not memory-safe. If using ARC, add in your #autoreleasePool{}; if not, don't forget your retain/release cycle.
I hope this helps!
This article shows how to create an app using UIPageViewController with custom viewcontrollers for each page: http://www.informit.com/articles/article.aspx?p=1760500&seqNum=6