首页 技术 正文
技术 2022年11月11日
0 收藏 410 点赞 3,267 浏览 3744 个字

前两天看了别人的文章,涉及到了镂空的展示,所以我在这里把实现的内容写成Swift语言的小Demo,供大家欣赏

首先,需要创建导航视图,然后创建两种展示方式的按钮

let vc = ViewController();
        let nav = UINavigationController.init(rootViewController: vc);
        window?.rootViewController = nav;

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.title = “两种镂空Demo”;
        self.view.backgroundColor = UIColor.whiteColor();
        
        //创建两个按钮
        let btn1 = UIButton.init(frame: CGRectMake(20, 100,screenWidth()-40, 50));
        btn1.setTitle(“滑块部分显示与背景不一样的图”, forState: .Normal);
        btn1.setTitleColor(UIColor.blueColor(), forState: .Normal);
        btn1.addTarget(self, action:#selector(btn1Click), forControlEvents: .TouchUpInside);
        self.view.addSubview(btn1);
        
        let btn2 = UIButton.init(frame: CGRectMake(20, 180,screenWidth()-40, 50));
        btn2.setTitle(“滑块部分显示与背景不一样的图”, forState: .Normal);
        btn2.setTitleColor(UIColor.blueColor(), forState: .Normal);
        btn2.addTarget(self, action:#selector(btn2Click), forControlEvents: .TouchUpInside);
        self.view.addSubview(btn2);
        
    }
    //按钮1方法
    func btn1Click(btn:UIButton) {
        let showVC = ShowViewController();
        showVC.typeInt = 1;
        self.navigationController?.pushViewController(showVC, animated: true);
    }
    //按钮2方法
    func btn2Click(btn:UIButton) {
        let showVC = ShowViewController();
        showVC.typeInt = 2;
        self.navigationController?.pushViewController(showVC, animated: true);
    }
    
    //相当于#define
    //返回屏幕宽
    func screenWidth() -> CGFloat {
        return UIScreen.mainScreen().bounds.size.width;
    }
    //返回屏幕高
    func screenHeight() -> CGFloat {
        return UIScreen.mainScreen().bounds.size.height;
    }

我分成了两种展示方式

一种是:滑块部分显示与背景不一样的图

//背景图片
            let backImage = UIImageView.init(frame: CGRectMake(0, 64, screenWidth(), screenHeight()-64));
            backImage.image = UIImage.init(named: “background.jpg”);
            self.view.addSubview(backImage);
            
            //背景视图
            bgView = UIView.init(frame: self.view.bounds);
            bgView.backgroundColor = UIColor.clearColor();
            self.view.addSubview(bgView);
            
            //移动视图
            clipView = UIView.init(frame: CGRectMake(0, 64, 180, 180));
            clipView.layer.cornerRadius = 90;
            clipView.layer.masksToBounds = true;
            //设置裁剪
            clipView.clipsToBounds = true;
            //设置交互
            clipView.userInteractionEnabled = true;
            bgView.addSubview(clipView);
            
            //显示图片
            showImgView = UIImageView.init(frame: CGRectMake(0, 0, screenWidth(), screenHeight()-64));
            showImgView.image = UIImage.init(named: “star.jpeg”);
            clipView.addSubview(showImgView);
            
            //添加拖动手势
            let panGresture = UIPanGestureRecognizer.init(target: self, action: #selector(panGrestureView));
            clipView.addGestureRecognizer(panGresture);

另一种是:滑块部分显示与背景不一样的图,背景是空白

bgView = UIView.init(frame: self.view.bounds);
            bgView.backgroundColor = UIColor.lightGrayColor();
            self.view.addSubview(bgView);
            
            //移动视图
            clipView = UIView.init(frame: CGRectMake(0, 64, screenWidth()-40, 120));
            //设置裁剪
            clipView.clipsToBounds = true;
            //设置交互
            clipView.userInteractionEnabled = true;
            bgView.addSubview(clipView);
            
            //显示图片
            showImgView = UIImageView.init(frame: CGRectMake(0, 0, screenWidth(), screenHeight()-64));
            showImgView.image = UIImage.init(named: “star.jpeg”);
            clipView.addSubview(showImgView);
            
            //添加拖动手势
            let panGresture = UIPanGestureRecognizer.init(target: self, action: #selector(panGrestureView));
            clipView.addGestureRecognizer(panGresture);

展示结果:

         

源码下载:http://download.csdn.net/detail/hbblzjy/9622850

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903