简体   繁体   中英

how to use CAShapeLayer inside scrollView

I have a scrollView inside which i create a new CAShapeLayer . I want myCAShapeLayer to be initial offscreen so i have to scroll down to see it.

This is in my scroll screen viewControler.m file:

[myScrollScreen setScrollEnabled:YES];
[myScrollScreen setContentSize:CGSizeMake(320, 1000)];
[myScrollScreen setFrame:CGRectMake(0, 0, 320, 568)]; 
// the view controller size is also set to freeform 320,1000
CAShapeLayer* myLayer;
myLayer = [[CAShapeLayer alloc] init];
CGPoint myCenter = CGPointMake(someX, Y_seeBellow);
myLayer.path = [UIBezierPath bezierPathWithArcCenter:myCenter radius:50 startAngle:-(M_PI/2.0) endAngle:3.0*M_PI/2.0 clockwise:YES].CGPath;

Now if I set Y to viewControler height * 0.5f - i can see myLayer in the middle of the screen. If I set Y to viewController height * 1.2f - i can't see myLayer when i scroll down (other things like buttons i can see this way). If I set Y to myScrollView.bounds or frame.size.height * 0.5f the Layer stays still - doesn't scroll. Any ideas?

Note that settings frame like that will no longer work on different size iPhone, i strongly recommend to use Autolayout instead.

First you need to add your shape layer inside UIScrollView . Your contentSize is basically place where you can put your shape.

You shouldn't modify your view controller height. Scroll view size should be set (if scrollview is fullscreen) as a same size as your ViewController. Then add a bezier path with Y_seeBellow to value self.view.frame.size.height+radius/2 .

Then your shape will be initialy right below the bottom of your visible part of scrollview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM