美文网首页实用轮子ios专题动画
iOS 实现类似擦玻璃效果的一种方法

iOS 实现类似擦玻璃效果的一种方法

作者: 萌小菜 | 来源:发表于2016-06-07 16:09 被阅读379次
效果图

首先谈下写这个demo的灵感来源(扯谈),是无意在github上看到这个东西(以下简称大神),感觉有意思就拿下来看看,结果看了好几遍也没完全看懂,再结合自己之前学的东西感觉不用这么复杂也能实现同样的效果,于是乎就开始动手了。

大体思路


动手前先想了下思路,就是利用母鸡哥讲的涂鸦 + 设置layer的mask的方式,这样做可以说是非常简单了。然后就用了半下午的时间写完了,效果基本和大神写得那个一样,而且对比了下代码量,我写得真是简单明了呀,用了不到大神代码量一半的代码就完成了同样的功能,心情愉悦。然后就想开开心心的把代码上传github,上传之前心里总感觉不太对,这么简单就能实现,大神为什么还要写得那么麻烦,而且还用到了很多c和c++的东西。然后我又跑了大神的应用看了看cpu利用率(我用5s跑的),大约最高保持在百分这十几,感觉有点高但也可以,再跑我自己写得,令我大吃了一惊,随便划几下就百分之40+了,这么个小东西耗这么多cpu那这也太low了,怎么还好意思上传到github上呢。。。

bug测试及解


经过测试,发现是母鸡哥讲的涂鸦有性能问题,虽然代码简单,思路清晰,但是随着触摸屏幕的点不断增加,整个绘制复杂度也是呈指数上升,导致的结果就是耗cpu非常严重。所以关于绘制图片我不得不再想其它的方法实现。但是我冥想了一天时间也没有找到好的方法降低绘制的复杂度(除了大神的那个方法),当然最后的解决方法也非常简单了,没错,就是copy大神的方法😂。下面着重介绍下大神的解决涂鸦cpu消耗问题方法(这里是重点):

  • 图形上下文:不再用layer的默认的图形上下文了(也就是在drawRect方法里面用UIGraphicsGetCurrentContext()获取的),而是自己创建一个全局的bitmap上下文
    self.imageContext = CGBitmapContextCreate(0, frame.size.width, frame.size.height, 8, frame.size.width * 4, self.colorSpace, kCGImageAlphaPremultipliedLast);
    CGContextSetStrokeColorWithColor(self.imageContext,[UIColor redColor].CGColor);
    CGContextSetFillColorWithColor(self.imageContext, [UIColor redColor].CGColor);
    CGContextTranslateCTM(self.imageContext, 0.0f, self.bounds.size.height);
    CGContextScaleCTM(self.imageContext, 1.0f, -1.0f);
  • 在触摸屏幕的时候(touchesBegantouchesMoved等方法),根据触摸的位置,每两个点之间连线,绘制到上面建立的图形上下文当中,这样就是随着触摸屏幕,随着往图形上下文绘制,不会把之前已经绘制的再重新添加绘制,解决了性能消耗过高的问题。
#pragma mark - touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];   
    [self reCreateImageWithTouchDict:@{@"touch":touch, @"lineWidth":@(touch.majorRadius)}];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];    
    [self reCreateImageWithTouchDict:@{@"touch":touch, @"lineWidth":@(touch.majorRadius)}];
}

- (UIImage *)reCreateImageWithTouchDict:(NSDictionary *)touchDict{
    UITouch* touch = touchDict[@"touch"];
    CGFloat lineWidth = [touchDict[@"lineWidth"] floatValue] * 0.5;
    if (lineWidth < 1.0) {
        lineWidth = 10;
    }  
    if (touch) {   
        CGPoint point = [touch locationInView:touch.view];
        if (touch.phase == UITouchPhaseBegan) {
            CGRect rect = CGRectMake(point.x - lineWidth, point.y - lineWidth, lineWidth*2, lineWidth*2);
            CGContextAddEllipseInRect(self.imageContext, rect);
            CGContextFillPath(self.imageContext);
            [self.points removeAllObjects];
            [self.points addObject:[NSValue valueWithCGPoint:point]];   
        }else if (touch.phase == UITouchPhaseMoved){
            [self.points addObject:[NSValue valueWithCGPoint:point]];
            if (self.points.count > 2) {
                CGContextSetLineCap(self.imageContext, kCGLineCapRound);
                CGContextSetLineWidth(self.imageContext, 2 * lineWidth);
                do{
                    CGPoint point0 = [(NSValue *)self.points[0] CGPointValue];
                    CGPoint point1 = [(NSValue *)self.points[1] CGPointValue];
                    CGContextMoveToPoint(self.imageContext, point0.x, point0.y);
                    CGContextAddLineToPoint(self.imageContext, point1.x, point1.y);
                    [self.points removeObjectAtIndex:0];
                }while (self.points.count > 2);       
            }
        }        
        CGContextStrokePath(self.imageContext);
    }  
    CGImageRef cgImage = CGBitmapContextCreateImage(self.imageContext);
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    return image;
}

最后实现


最后设置mask就非常简单了,设置我们将要显示的图片(那张清晰的)的layer的mask为上面通过绘制生成的image的layer,这样只有绘制过的位置才能看到将要显示的图片,功能就完成了😀,我感觉利用这个小技巧可以做很多有趣的东西(类似刮奖等)

    CALayer *mask = [CALayer layer];
    mask.contents = (id)image.CGImage;
    mask.anchorPoint = CGPointZero;
    mask.frame = self.bounds;
    self.imageView.layer.mask = mask;
    self.imageView.layer.masksToBounds = YES;

最后


别忘记释放相关内存

- (void)dealloc{
    if (_imageContext != NULL) {
        CFRelease(_imageContext);
    }
    
    if (_colorSpace != NULL) {
        CFRelease(_colorSpace);
    }
}

最后的最后 奉上demo地址
有错误的地方欢迎评论指正

相关文章

  • iOS 实现类似擦玻璃效果的一种方法

    首先谈下写这个demo的灵感来源(扯谈),是无意在github上看到这个东西(以下简称大神),感觉有意思就拿下来看...

  • 安卓模糊效果探讨

    在项目中我们或多或少的需要根据产品的要求,实现类似于ios的毛玻璃效果。在ios中毛玻璃的实现,比较简单,系统提供...

  • iOS之原生毛玻璃效果

    iOS 8之后 iOS提供了实现原生毛玻璃效果的接口,使用起来也非常方便,先简单看看效果图 使用方法 在添加毛玻璃...

  • iOS毛玻璃效果的实现方法

    ios开发中常常用到的毛玻璃效果实现方法 iOS8以后使用系统里的UIBlurEffect可以实现,UIBlurE...

  • 实现div毛玻璃背景

    原文在我的博客☞实现div毛玻璃背景 毛玻璃效果 ios里毛玻璃效果的使用非常多,本文介绍一个实现div毛玻璃背景...

  • iOS的毛玻璃效果

    简要说明 毛玻璃(高斯模糊)效果是iOS开发中经常使用到的一个实现模糊效果的技能,实现这个效果有三种方法: 在iO...

  • 记忆中的年味||擦玻璃

    最近天气好,勤快的阿姨说要把玻璃都擦了。 因为是高层,玻璃还是双层,擦玻璃需要用专门工具——擦玻璃器。 是一种类似...

  • iOS毛玻璃效果

    iOS7以后,苹果公司提供了一种实时毛玻璃的渲染效果,苹果提供了一些方法让我们可以在UIImage上增加毛玻璃效果...

  • flutter_bottom_sheet 类似iOS底部弹框实现

    flutter实现类似iOS底部弹框效果,背景颜色可能需要调整,只是提供一种实现思路,大家可以相互学习下载地址 ...

  • ios实现毛玻璃效果

    我们开发中可能经常会使用到给图片添加毛玻璃效果,ios7以后毛玻璃效果就开始比较多的使用了,系统提供了一种简单实现...

网友评论

本文标题:iOS 实现类似擦玻璃效果的一种方法

本文链接:https://www.haomeiwen.com/subject/zsiddttx.html