图像旋转,覆盖按钮

时间:2012-02-19 15:37:58

标签: objective-c

我想从左到右围绕x轴旋转图像。问题是,当您旋转图像时,会覆盖位于顶部的按钮

运行动画

   [AnimationUtil rotationRightToLeftForView:image andDuration:1]; 

Animation metod


+(void) rotationRightToLeftForView:(UIView *)flipView andDuration:(NSTimeInterval)duration{

// Remove existing animations before stating new animation [flipView.layer removeAllAnimations]; // Make sure view is visible flipView.hidden = NO; // show 1/2 animation //flipView.layer.doubleSided = NO; // disable the view so it’s not doing anythign while animating flipView.userInteractionEnabled = NO; // Set the CALayer anchorPoint to the left edge and // translate the button to account for the new // anchorPoint. In case you want to reuse the animation // for this button, we only do the translation and // anchor point setting once. if (flipView.layer.anchorPoint.x != 0.0f) { flipView.layer.anchorPoint = CGPointMake(0.0f, 0.5f); flipView.center = CGPointMake(flipView.center.x-flipView.bounds.size.width/2.0f, flipView.center.y); } // create an animation to hold the page turning CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; transformAnimation.removedOnCompletion = NO; transformAnimation.duration = duration; transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // start the animation from the current state transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; // this is the basic rotation by 180 degree along the y-axis M_PI CATransform3D endTransform = CATransform3DMakeRotation(radians(180.0), 0.0f, -1.0f, 0.0f); transformAnimation.toValue = [NSValue valueWithCATransform3D:endTransform]; // Create an animation group to hold the rotation CAAnimationGroup *theGroup = [CAAnimationGroup animation]; // Set self as the delegate to receive notification when the animation finishes theGroup.delegate = self; theGroup.duration = duration; // CAAnimation-objects support arbitrary Key-Value pairs, we add the UIView tag // to identify the animation later when it finishes [theGroup setValue:[NSNumber numberWithInt:flipView.tag] forKey:@"viewFlipTag"]; // Here you could add other animations to the array theGroup.animations = [NSArray arrayWithObjects:transformAnimation, nil]; theGroup.removedOnCompletion = NO; // Add the animation group to the layer [flipView.layer addAnimation:theGroup forKey:@"flipView"];

}

1 个答案:

答案 0 :(得分:0)

决定如下: 创建蒙版(图像)颜色=黑色,制作区域大小= Button.size颜色=透明 image name =“mask2.png”

button = ...;
ImageView = ...;
parent_view = ...;

UIImage *_maskingImage = [UIImage imageNamed:@"mask2"];
CALayer *_maskingLayer = [CALayer layer];
_maskingLayer.frame = parent_View.bounds;
[_maskingLayer setContents:(id)[_maskingImage CGImage]];

CALayer *layerForImage = [[CALayer alloc] init];
layerForImage.frame = parent_View.bounds;
[layerForImage setMask:_maskingLayer];
[layerForImage addSublayer:ImageView.layer];

[parent_View.layer addSublayer:layerForImage];
[parent_View addSubView:button];