我想根据滚动缩放(缩小)个人资料图片。但是,最重要的是,我希望不是从中心点而是从右下角缩放它。意味着应该固定右下角,图像应该从左边和上边缘收缩,如图所示:
—————————————————
| |
| ———————————-
| | |
| | |
| | —————|
| | | |
| | | |
—————————————————
这就是我在做的事情。个人资料照片名为imgVwProfilePic
:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offset = scrollView.contentOffset.y;
CATransform3D avatarTransform = CATransform3DIdentity;
CATransform3D headerTransform = CATransform3DIdentity;
viewHeader.alpha = 1.0;
// PULL DOWN -----------------
if(offset < 0)
{
CGFloat headerScaleFactor = -(offset) / CGRectGetHeight(viewHeader.bounds);
int headerSizevariation = ((CGRectGetHeight(viewHeader.bounds) * (1.0 + headerScaleFactor)) - CGRectGetHeight(viewHeader.bounds))/2.0;
headerTransform = CATransform3DTranslate(headerTransform, 0, headerSizevariation, 0);
headerTransform = CATransform3DScale(headerTransform, 1.0 + headerScaleFactor, 1.0 + headerScaleFactor, 0);
viewHeader.layer.transform = headerTransform;
}
// SCROLL UP/DOWN ------------
else
{
CGFloat offset_HeaderStop = viewHeader.frame.size.height;
headerTransform = CATransform3DTranslate(headerTransform, 0, MAX(-offset_HeaderStop, -offset), 0);
float avatarScaleFactor = (MIN(offset_HeaderStop, offset)) / CGRectGetHeight(imgVwProfilePic.bounds) / 3.8; //1.4// Slow down the animation
float avatarSizeVariation = ((CGRectGetHeight(imgVwProfilePic.bounds) * (1.0 + avatarScaleFactor)) - CGRectGetHeight(imgVwProfilePic.bounds)) / 2.0;
avatarTransform = CATransform3DTranslate(avatarTransform, 0, avatarSizeVariation, 0);
avatarTransform = CATransform3DScale(avatarTransform, 1.0 - avatarScaleFactor, 1.0 - avatarScaleFactor, 0);
if(offset <= offset_HeaderStop){
if(imgVwProfilePic.layer.zPosition < viewHeader.layer.zPosition){
viewHeader.layer.zPosition = 0;
}
}
else {
if (imgVwProfilePic.layer.zPosition >= viewHeader.layer.zPosition){
viewHeader.layer.zPosition = 2;
}
}
}
// Apply Transformations
viewHeader.layer.transform = headerTransform;
imgVwProfilePic.layer.transform = avatarTransform;
}
现在,我知道我必须为该图像视图设置锚点。但我不知道我会在哪里设置它。我在viewDidLoad
中设置了它,但它更改了该图像视图的原始帧。
- (void)viewDidLoad
{
imgVwProfilePic.layer.anchorPoint = CGPointMake(1, 1); // 1,1 for bottom right corner...
}
答案 0 :(得分:1)
你必须设置锚位置(IIRC是中心的相对X和相对Y位置的[0.0,1.0]范围内的一对标量)。
之后,&#34;位置&#34;图层的属性(以及扩展名为视图的原点)将为w.r.t.那个锚位置,所以你必须重新调整帧。您可以尝试覆盖
- (void) setFrame:(CGRect)frame {
CGPoint center = CGPointMake ( frame.origin.x+0.5*frame.size.width , frame.origin.y+0.5*frame.size.height ) ;
viewHeader.layer.position = center ;
CGRect bounds = CGRectMake ( 0,0 , frame.size.width,frame.size.height ) ;
viewHeader.layer.bounds = bounds ;
}
让它照顾好。