首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Objective C中设置遮罩动画

在Objective C中设置遮罩动画
EN

Stack Overflow用户
提问于 2013-07-31 00:39:11
回答 1查看 1.8K关注 0票数 1

我正在制作一个应用程序,我必须在一个单一的屏幕上显示3个图像,当用户触摸一个图像,然后该图像应通过动画和大小显示。

所以对于第一步,我用来自How to Mask an UIImageView的3个不同的透明蒙版图像对3张图像进行了掩模

我的方法如下

代码语言:javascript
复制
- (UIImageView*) maskedImage:(UIImage *)image withMasked:(UIImage *)maskImage {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CALayer *mask1 = [CALayer layer];
    mask1.contents = (id)[maskImage CGImage];
    mask1.frame = CGRectMake(0, 0, 1024, 768);
    imageView.layer.mask = mask1;
    imageView.layer.masksToBounds = YES;
    return imageView;
}

我把这个叫做

代码语言:javascript
复制
    self.image2 = [UIImage imageNamed:@"screen2Full.png"];
    self.mask2 = [UIImage imageNamed:@"maskImage.png"];
    self.imageView2 = [self maskedImage:self.image2 withMasked:self.mask2];
    [self.completeSingleView addSubview:self.imageView2];

这是完美的工作。

现在进入第二步。动画蒙版,这样就可以显示完整的图像。我在谷歌上搜索了很多关于这方面的信息,但都没有成功。所以请帮帮我。我只想知道我们如何动画和调整蒙版图像的大小,以便我可以查看完整的图像。我的想法如下。

EN

回答 1

Stack Overflow用户

发布于 2013-07-31 18:11:54

因此,我最终从多个站点和一些自己的逻辑和努力工作中得到了解决方案。下面是这个问题的解决方案

代码语言:javascript
复制
-(void)moveLayer:(CALayer*)layer to:(CGPoint)point
{
    // Prepare the animation from the current position to the new position
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [layer valueForKey:@"position"];
    animation.toValue = [NSValue valueWithCGPoint:point];
    animation.duration = .3;
    layer.position = point;

    [layer addAnimation:animation forKey:@"position"];
}

-(void)resizeLayer:(CALayer*)layer to:(CGSize)size
{
    CGRect oldBounds = layer.bounds;
    CGRect newBounds = oldBounds;
    newBounds.size = size;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds"];

    animation.fromValue = [NSValue valueWithCGRect:oldBounds];
    animation.toValue = [NSValue valueWithCGRect:newBounds];
    animation.duration = .3;
    layer.bounds = newBounds;    
    [layer addAnimation:animation forKey:@"bounds"];
}

- (UIImageView*) animateMaskImage:(UIImage *)image withMask:(UIImage *)maskImage width:(int )wd andHeight:(int )ht andSize:(CGSize)size andPosition:(CGPoint)pt {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    CALayer *mask = [CALayer layer];
    mask.contents = (id)[maskImage CGImage];
    mask.frame = CGRectMake(0, 0, wd, ht);

    //This needs to be place before masking. I don't knwo why. But its working :)
    [self resizeLayer:mask to:size];
    [self moveLayer:mask to:pt];

    imageView.layer.mask = mask;
    imageView.layer.masksToBounds = YES;
    return imageView;
}

您只需将其命名为

代码语言:javascript
复制
 [self.imageView1 removeFromSuperview];
 self.imageView1 = [self animateMaskedImage:self.image1 withMasked:self.mask1 width:1024 andHeight:768 andSize:CGSizeMake(1024*4, 768*4) andPosition:CGPointMake(1024*2, 768*2)];
 [self.completeSingleView addSubview:self.imageView1];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17952702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档