将运动效果添加到图层的蒙版

时间:2013-10-13 15:52:58

标签: ios

我有一个金色的UILabel(shineLabel),我希望在用户倾斜设备时突出显示。想想iOS 6的Music.app音量旋钮,但是需要掩码的文字。

目前,掩码占用了视图的layer.mask属性。当我在没有运动控制的情况下为此图层设置动画时,它的工作方式完全符高亮显示遍历文本,并被字符的形状掩盖。

现在我正在尝试实现iOS 7的addMotionEffect:按照these instructions但没有运气动画只是掩码。带掩码的图层的代码:

self.shineLabel.font = [UIFont fontWithName:@"GillSans-Light" size:28];
self.shineLabel.backgroundColor = [UIColor clearColor];
self.shineLabel.text = @"Hello";

[self addSubview:self.shineLabel];

// Create the mask and assign it to the shineLabel's layer.mask property
CALayer *maskLayer = [CALayer layer];

maskLayer.backgroundColor = [[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0] CGColor];
maskLayer.contents = (id)[[UIImage imageNamed:@"highlight.png"] CGImage];

maskLayer.contentsGravity = kCAGravityCenter;
maskLayer.frame = CGRectMake(-self.journalLabelTitle.frame.size.width - 40, 0.0f, self.shineLabel.frame.size.width * 2, self.shineLabel.frame.size.height);

self.shineLabel.layer.mask = maskLayer;

代码动画并对设备的动作作出反应:

// Set horizontal effect
UIInterpolatingMotionEffect *horizontalMotionEffect =
[[UIInterpolatingMotionEffect alloc]
 initWithKeyPath:@"layer.mask.position.x"
 type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
horizontalMotionEffect.minimumRelativeValue = @(-40.0f);
horizontalMotionEffect.maximumRelativeValue = @(40.0f);

// Add effect to your view
[self.shineLabel addMotionEffect:horizontalMotionEffect];

如果我将initWithKeyPath:“layer.mask.position.x”更改为“layer.position.x”,我可以移动视图的图层,但似乎我试图只移动layer.mask我不能

有人能帮我解决这个问题吗?

0 个答案:

没有答案