Swift基础之实现一个镂空图片的小Demo

来源:互联网 发布:东非解放军 知乎 编辑:程序博客网 时间:2024/03/29 14:30

前两天看了别人的文章,涉及到了镂空的展示,所以我在这里把实现的内容写成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



0 0
原创粉丝点击