UI控件之UISlider简单图片放大与缩小的应用

//  UISlider实例

//

//  Created by Catherine on 2017/8/29.

//  Copyright © 2017 Catherine. All rights reserved.

//


import UIKit


class ViewController: UIViewController {

    let imageview:UIImageView = UIImageView(frame: CGRect(x: 30, y: 50, width: 320, height: 400))

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let slider:UISlider = UISlider(frame: CGRect(x: 70, y: self.view.frame.size.height - 60, width: 280, height: 20))

        slider.addTarget(self, action: #selector(slider(slider:)), for: UIControlEvents.valueChanged)

        imageview.image = UIImage(named: "pig1.png")

        slider.maximumValue = 100

        slider.minimumValue = 0

        self.view.addSubview(slider)


        self.view.addSubview(imageview)

        

    }

    //通过slider调整大小

    func slider(slider:UISlider){

        let value:float_t = 1-slider.value/100

        //修改bounds是以中间为基准缩小的   如果不改变x,y值那么frame是以左上角

        imageview.bounds = CGRect(x: 0, y: 0, width: 320*(CGFloat)(value), height: 400*(CGFloat)(value))

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}

你可能感兴趣的:(swift初步)