ios学习之模糊效果的学习

本文介绍了一种在iOS应用中实现模糊背景效果的方法。通过使用UIBlurEffect和UIVisualEffectView,可以轻松地为UIImageView添加轻量级模糊效果,并通过设置图标的圆角大小和裁剪属性来完善视觉体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看了老郑的模糊效果,所以自己也做了一个,感觉还不错

用到以下知识


随后是代码部分的关联

一个是背景imageView.还有一个是前面的imageVIew

@IBOutlet var bg: UIImageView!

    @IBOutlet var tuBiao: UIImageView!
随后在viewDIdLoad中添加如下代码

  //设置图标的圆角大小
        tuBiao.layer.cornerRadius = 50
        //这句话一定要设为true,否则无法出现圆角设置
        tuBiao.clipsToBounds = true
        //创建模糊效果实例,模糊效果的样式会有所不同
        let alert = UIBlurEffect(style: UIBlurEffectStyle.Light)
        //创建效果图实例,继承自模糊效果实例
        let effectView = UIVisualEffectView(effect: alert)
        //设置效果图的尺寸大小
        effectView.frame.size = CGSizeMake(view.frame.size.width, view.frame.size.height)
        //将view添加到自己的视图中
        bg.addSubview(effectView)
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
整体的代码如下

//
//  ViewController.swift
//  shiyan25
//
//  Created by HISE_CS on 15/10/13.
//  Copyright © 2015年 HISE_CS. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet var bg: UIImageView!

    @IBOutlet var tuBiao: UIImageView!
    override func viewDidLoad() {
        //设置图标的圆角大小
        tuBiao.layer.cornerRadius = 50
        //这句话一定要设为true,否则无法出现圆角设置
        tuBiao.clipsToBounds = true
        //创建模糊效果实例,模糊效果的样式会有所不同
        let alert = UIBlurEffect(style: UIBlurEffectStyle.Light)
        //创建效果图实例,继承自模糊效果实例
        let effectView = UIVisualEffectView(effect: alert)
        //设置效果图的尺寸大小
        effectView.frame.size = CGSizeMake(view.frame.size.width, view.frame.size.height)
        //将view添加到自己的视图中
        bg.addSubview(effectView)
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

最后的效果图如图



感觉还是不错的大笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值