CALayer on CATiledLayer, drawLayer:inContext never called - iphone

i have a UIView which display a PDF page using CATiledLayer, now i want to add another CALayer on TiledLayer to draw some annots, Please see the code below.
+ (Class)layerClass
{
return [TiledLayer class];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
TiledLayer *tiledLayer = (id)self.layer;
tiledLayer.backgroundColor = [UIColor whiteColor].CGColor;
NSAssert([tiledLayer isKindOfClass:[TiledLayer class]], #"self.layer must be CATiledLayer");
drawingLayer = [CALayer layer];
drawingLayer.frame = frame;
//its crashing if set the delegate, if not drawLayer is never called.
[drawingLayer setDelegate:self];
[self.layer addSublayer:self.drawingLayer];
}
}
- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context
{
if(layer == [self layer])
{
[self drawPDFPage];
[drawingLayer setNeedsDisplay];
}
else if(layer == drawingLayer)
{
//this one is never called.
[self drawSomethinghere];
}
}

UIView uses the existence of -drawRect: to determine if it should allow its CALayer to be invalidated, which would then lead to the layer creating a backing store and -drawLayer:inContext: being called.
Implementing an empty -drawRect: method allows UIKit to continue to implement this logic, while doing the real drawing work inside of -drawLayer:inContext:.
Just add to you class implementation:
-(void)drawRect:(CGRect)r
{
}
And your drawLayer method should work.

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
}

GLKView set drawable properties

I'm trying to port Apples GLPaint example to use GLKit. Using a UIView, its possible to return the CAEAGLLayer of the view and set the drawableProperties to include kEAGLDrawablePropertyRetainedBacking. This has the effect of retaining the drawable contents after presenting the render buffer, as expected. Removing this property results in flickering after the draw call with part of the drawable content seemingly being drawn to different buffers.
The problem is this is exactly the issue I am now having in my GLKView, but there doesn't seem to be a way to set the drawable properties. Returning a CAEAGLLayer and setting the properties has no effect and I don't see any relevant properties of GLKView to set retained backing.
Has anybody else come across this or have a solution?
If you want to get kEAGLDrawablePropertyRetainedBacking in a GLKView, add the following category to your project.
#interface CAEAGLLayer (Retained)
#end
#implementation CAEAGLLayer (Retained)
- (NSDictionary*) drawableProperties
{
return #{kEAGLDrawablePropertyRetainedBacking : #(YES)};
}
#end
Setting the drawableProperties on the CAEAGLLayer maintained by the GLKView doesn't work because the GLKView overwrites those properties when it binds its drawable and generates its render storage. Using this method forces the GLKView to use your category's returned drawableProperties instead.
Simeon's answer works but changes the behavior for all EAGL-based views in an application. I have some views which need the backing forced and others which don't, so I came up with a slightly different solution by creating subclasses of GLKView and CEAGLLayer, like this:
#interface RetainedEAGLLayer : CAEAGLLayer
#end
#implementation RetainedEAGLLayer
- (void)setDrawableProperties:(NSDictionary *)drawableProperties {
// Copy the dictionary and add/modify the retained property
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithCapacity:drawableProperties.count + 1];
[drawableProperties enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL *stop) {
// Copy all keys except the retained backing
if (![key isKindOfClass:[NSString class]]
|| ![(NSString *)key isEqualToString:kEAGLDrawablePropertyRetainedBacking])
[mutableDictionary setObject:object forKey:key];
}];
// Add the retained backing setting
[mutableDictionary setObject:#(YES) forKey:kEAGLDrawablePropertyRetainedBacking];
// Continue
[super setDrawableProperties:mutableDictionary];
[mutableDictionary release];
}
#end
and this
#interface RetainedGLKView : GLKView
#end
#implementation RetainedGLKView
+ (Class)layerClass {
return [RetainedEAGLLayer class];
}
#end
Now I can just use RetainedGLKView instead of GLKView for those views where I want to force a retained backing.
Not sure if this will work but here is some code we have:
GLKView * const view = (GLKView *)self.view;
view.context = self.context;
view.delegate = self;
view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.drawableMultisample = GLKViewDrawableMultisampleNone;
self.preferredFramesPerSecond = 30;
[EAGLContext setCurrentContext:self.context];
CAEAGLLayer * const eaglLayer = (CAEAGLLayer*) view.layer;
eaglLayer.opaque = YES;
You should be able to access eaglLayer.drawableProperties. Hopefully that lets you set the parameter you want.
In your GLKView implementation file:
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
_eaglLayer = (CAEAGLLayer *)self.layer;
_eaglLayer.opaque = TRUE;
_eaglLayer.drawableProperties = #{ kEAGLDrawablePropertyRetainedBacking : [NSNumber numberWithBool:NO],
kEAGLDrawablePropertyColorFormat : kEAGLColorFormatRGBA8};
}
return self;
}
I don't know how people manage to make things so complicated; it's like this, and only this.
Hi Please try this one
GLKView * const view = (GLKView *)self.view;
view.context = self.context;
view.delegate = self;
view.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
view.drawableMultisample = GLKViewDrawableMultisampleNone;
self.preferredFramesPerSecond = 10;
[EAGLContext setCurrentContext:self.context];
CAEAGLLayer * const eaglLayer = (CAEAGLLayer*) view.layer;

CGContextDrawImage without Transaction animation

How do I draw a layer without a transaction animation? For example, when I set the contents of a layer using CATransaction it works well:
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
myLayer.contents = (id)[[imagesTop objectAtIndex:Number]CGImage];
[CATransaction commit];
but I need to change contents from the delegate method [myLayer setNeedsDisplay]. Here is the code:
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
CGContextDrawImage(context, rect, Image.CGImage]);
}
CALayer implements setNeedsDisplay method:
Calling this method will cause the receiver to recache its content. This will result in the layer receiving a drawInContext: which may result in the delegate receiving either a displayLayer: or drawLayer:inContext: message.
... and displayIfNeeded:
When this message is received the layer will invoke display if it has been marked as requiring display.
If you wrap [myLayer displayIfNeeded] in a CATransaction, you can turn off implicit animation.
You can subclass CALayer and override actionForKey:,
- (id <CAAction>) actionForKey: (NSString *) key
{
if ([key isEqualToString: #"contents"])
return nil;
return [super actionForKey: key];
}
This code disables the built-in animation for the contents property.
Alternatively, you can achieve the same effect by implementing the layer's delegate method
- (id <CAAction>) actionForLayer: (CALayer *) layer forKey: (NSString *) key
{
if (layer == myLayer) {
if ([key isEqualToString: #"contents"])
return nil;
return [[layer class] defaultActionForKey: key];
}
return [layer actionForKey: key];
}

Is - [UIView (MyOwnCategory) drawRect:] never called on 3.1.3?

I define my own my own drawRect method and it is called on 4.2.1 (iOS) 5.0 (iOS) and 4.3.2 (Simulator) sucesfully. But it never called on 3.1.3 (iPhone 2g).
What reason could be for this?
P.S. Since i start write the question i think about my 3.1.3 device is jailbroken. Maybe it is root cause of this strange behaviour.
Upd: To reproduce issue i use next code:
#implementation UIView (MyOwnCategory)
- (void)drawRect:(CGRect)rect
{
const char * function = __FUNCTION__;
[NSException raise: #"hi!" format: #"%s", function];
}
#end
exception never happened on 3.1.3 even when i call [super drawRect: rect] explicitly
I wanted to write about Method Swizzling for a few weeks now, and #Kevin Ballard's comment finally made me do it (thank you for the inspiration, Kevin).
So here's a solution for your problem using method swizzling which should also work on iOS 3.x:
UIView+Border.h:
#import <Foundation/Foundation.h>
#interface UIView(Border)
#end
UIView+Border.m:
#import "UIView+Border.h"
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
#implementation UIView(Border)
- (id)swizzled_initWithFrame:(CGRect)frame
{
// This is the confusing part (article explains this line).
id result = [self swizzled_initWithFrame:frame];
// Safe guard: do we have an UIView (or something that has a layer)?
if ([result respondsToSelector:#selector(layer)]) {
// Get layer for this view.
CALayer *layer = [result layer];
// Set border on layer.
layer.borderWidth = 2;
layer.borderColor = [[UIColor redColor] CGColor];
}
// Return the modified view.
return result;
}
- (id)swizzled_initWithCoder:(NSCoder *)aDecoder
{
// This is the confusing part (article explains this line).
id result = [self swizzled_initWithCoder:aDecoder];
// Safe guard: do we have an UIView (or something that has a layer)?
if ([result respondsToSelector:#selector(layer)]) {
// Get layer for this view.
CALayer *layer = [result layer];
// Set border on layer.
layer.borderWidth = 2;
layer.borderColor = [[UIColor blueColor] CGColor];
}
// Return the modified view.
return result;
}
+ (void)load
{
// The "+ load" method is called once, very early in the application life-cycle.
// It's called even before the "main" function is called. Beware: there's no
// autorelease pool at this point, so avoid Objective-C calls.
Method original, swizzle;
// Get the "- (id)initWithFrame:" method.
original = class_getInstanceMethod(self, #selector(initWithFrame:));
// Get the "- (id)swizzled_initWithFrame:" method.
swizzle = class_getInstanceMethod(self, #selector(swizzled_initWithFrame:));
// Swap their implementations.
method_exchangeImplementations(original, swizzle);
// Get the "- (id)initWithCoder:" method.
original = class_getInstanceMethod(self, #selector(initWithCoder:));
// Get the "- (id)swizzled_initWithCoder:" method.
swizzle = class_getInstanceMethod(self, #selector(swizzled_initWithCoder:));
// Swap their implementations.
method_exchangeImplementations(original, swizzle);
}
#end

Cocos2d removeChild not working

// 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])) {
buttonPressed = NO;
CCMenuItem *myMenuItem = [CCMenuItemImage itemFromNormalImage:#"Icon-72.png" selectedImage:#"Icon-Small.png"target:self selector:#selector(menuSelector:)];
CCMenu *myMenu = [CCMenu menuWithItems:myMenuItem, nil];
myMenu.position = ccp(50, 50);
// add the label as a child to this Layer
[self addChild: myMenu];
}
return self;
}
-(void)menuSelector:(id)sender{
CCSprite *mySprite = [CCSprite spriteWithFile:#"Icon.png"];
mySprite.position = ccp(100, 100);
if (!buttonPressed) {
buttonPressed = YES;
[self addChild:mySprite];
}
else{
[self removeChild:mySprite cleanup:YES];
buttonPressed = NO;
}
}
Why is removeChild:mySprite not working? mySprite still there after i pressed the button(myMenuItem) the second time. Please help me. Thanks.
Every time you touch the button you're creating a new instance of the sprite. You're probably better off creating and adding it as a class variable so you can access it in all methods, then just setting it's visibility on or off as needed.