Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAScrollLayer doesn't scroll!

Maybe it's because it's late. Whatever the reason I can't figure out why I'm having trouble with a simple CSScrollLayer example I'm trying. I add a 50 pixel Eclipse icon to a view based project and in my initialize method (called from initWithNibName:bundle:) I have this:

-(void) initialize
{
  CAScrollLayer *scrollLayer = [CAScrollLayer layer];
  scrollLayer.backgroundColor =  [[UIColor blackColor] CGColor];
  CGRect bounds = self.view.bounds;
  scrollLayer.bounds = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
  scrollLayer.contentsRect = CGRectMake(0, 0, bounds.size.width + 800, bounds.size.height + 800);
  scrollLayer.borderWidth = 2.5;
  scrollLayer.borderColor = [[UIColor redColor] CGColor];

  scrollLayer.position = CGPointMake(self.view.center.x, self.view.center.y - 20);
  scrollLayer.scrollMode = kCAScrollBoth;
  [self.view.layer addSublayer:scrollLayer];
  UIImage *image = [UIImage imageNamed:@"eclipse32.gif"];
  for(int i=0; i<6; i++) {
    layer = [CALayer layer];
    layer.backgroundColor = [[UIColor blackColor] CGColor];
    layer.bounds = CGRectMake(0, 0, 100, 100);
    layer.contents = (id)[image CGImage];
    layer.position = CGPointMake(layer.bounds.size.width * i, self.view.center.y);
    [scrollLayer addSublayer:layer];
  }
//  [button removeFromSuperview];
//  [self.view addSubview:button];
//  self.view.userInteractionEnabled = YES;
  [image release];
}

The scroll layer shows, the icon is repeated on the layer I have a border around the edge of the screen. Everything is lovely except I can't scroll the icons. I've tried with/without setting scroll mode. I've tried with a single stretched icon that falls off screen. I've tried everything. What am I doing wrong?

like image 483
Cliff Avatar asked Nov 27 '25 08:11

Cliff


1 Answers

CAScrollLayer does not plug into the UIEvent chain so you need to manually add code to modify the scroll offsets on touch events. It is also very tricky to get the scrolling momentum like UIScrollView does. I would strongly recommend re-implementing with UIScrollView -- it will be remarkably simpler. The only other option is to do manual touch event handling, which is painful! I tried to get a nice scroller with CAScrollLayer and gave up after implementing a few awkward scrolling behaviors manually.

The general rule I follow with using CALayer or UIView's is to use CALayer objects when I don't need any user interaction. If the user is going to touch it, use the UIView, it's a very lightweight object so the overhead is negligible.

That said, I've found CAScrollLayer remarkably useless! Why not just use a CALayer and modify the bounds? There's probably some use to it, but it is no replacement for UIScrollView.

like image 197
Brian King Avatar answered Nov 29 '25 23:11

Brian King



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!