Fork me on GitHub

改变CALayer的anchorPoint使视图缩放时其中一边固定不动


最近遇到个需求,需要当手指向上划动时视图向上移动显示出隐藏在屏幕下方的菜单,这时整个视图的bounds需要大于屏幕大小,隐藏菜单才能接收到Touch事件,但是发现无论在loadView里初始化view的bounds为多大,最后它会自动变为屏幕大小。于是只能在swipe操作中处理了。


但是如果直接self.view.bounds = aBounds的话,会看到视图是以中心点进行伸缩的,而我需要的是以最上方为基点进行伸缩,解决方法是改变layer的anchorPoint,然后将layer的位置固定。


anchorPoint是CALayer的定位点,也就是设置position时对准的点,默认的anchorPoint是中心点。


 


 


 - (void)loadView



    CGRect frame = [UIScreen mainScreen].applicationFrame;


    self.view = [[[UIView allocinitWithFrame:frame] autorelease];


 


    CALayer *layer = self.view.layer;


    layer.anchorPoint = CGPointMake(0.50);    //Top-middle


    layer.position = CGPointMake(kSelfViewWidth/20);


}


 


 


- (void)swipe:(UISwipeGestureRecognizer *)g{


   __block CGRect bounds = self.view.bounds;


    float h = 520.0;


    bounds.size.height = h;


 


    CALayer *layer = self.view.layer;


    layer.bounds = bounds;


    ...


}

http://danal.blog.51cto.com/3353275/729134

posted on 2012-05-24 09:29  pengyingh  阅读(276)  评论(0)    收藏  举报

导航