Search code examples
objective-ciosif-statementcore-animationcalayer

If statement to choose between multiple CALayer


I have a Core Animation image on boxLayer and I'm duplicating it, changing the action of and shifting the position of the 2nd (boxLayer2) so that someone can choose between the 2.

I want the user to be able to tap the image for boxLayer and the boxLayer2 image does nothing but boxLayer moves (I didn't include my animation code beyond receiving the touch) and viceversa.

I cannot get an if statement to work. I've tried multiple variations self.layer == boxLayer or CALayer == boxlayer ... sublayer is an array so that's out. Any help/explanation as I know I'm missing something would be greatly appreciated.

Thanks!

UIView *BounceView is declared in the VC

In BounceView I have 2 CALayers declared: boxlayer & boxlayer2

BounceView.m

- (id)initWithFrame:(CGRect)frame       
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];


        // Create the new layer object
        boxLayer = [[CALayer alloc] init];
        boxLayer2 = [[CALayer alloc] init];

        // Give it a size
        [boxLayer setBounds:CGRectMake(0.0, 0.0, 185.0, 85.0)];
        [boxLayer2 setBounds:CGRectMake(0.0, 0.0, 185.0, 85.0)];

        // Give it a location
        [boxLayer setPosition:CGPointMake(150.0, 140.0)];
        [boxLayer2 setPosition:CGPointMake(150.0, 540.0)];

        // Create a UIImage
        UIImage *layerImage = [UIImage imageNamed:@"error-label.png"];
        UIImage *layerImage2 = [UIImage imageNamed:@"error-label.png"];

        // Get the underlying CGImage
        CGImageRef image = [layerImage CGImage];
        CGImageRef image2 = [layerImage2 CGImage];

        // Put the CGImage on the layer
        [boxLayer setContents:(__bridge id)image];
        [boxLayer2 setContents:(__bridge id)image2];

        // Let the image resize (without changing the aspect ratio) 
        // to fill the contentRect
        [boxLayer setContentsGravity:kCAGravityResizeAspect];
        [boxLayer2 setContentsGravity:kCAGravityResizeAspect];


        // Make it a sublayer of the view's layer
        [[self layer] addSublayer:boxLayer];
        [[self layer] addSublayer:boxLayer2];

    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches
           withEvent:(UIEvent *)event
{
  if (CAlayer == boxLayer)
  {
  // do something
  }

  else
  {
  // do something else
  }
}

Solution

  • It looks to me like you are trying to know what layer the user tapped on inside touched began and that this is your problem.

    How to find out what layer was tapped

    CALayer has an instance method - (CALayer *)hitTest:(CGPoint)thePoint that

    Returns the farthest descendant of the receiver in the layer hierarchy (including itself) that contains a specified point.

    So to find out what layer you tapped you should do something like

    - (void)touchesBegan:(NSSet *)touches
               withEvent:(UIEvent *)event {
        UITouch *anyTouch = [[event allTouches] anyObject];
        CGPoint pointInView = [anyTouch locationInView:self];
    
        // Now you can test what layer that was tapped ...
        if ([boxLayer hitTest:pointInView]) {
            // do something with boxLayer
        } 
        // the rest of your code
    }
    

    This works because hitTest will return nil if the point is outside the layer's bounds.