Swift開發之UIButton作為蒙板實現圖片縮放,swiftuibutton
很多夥伴們們總是認為縮放圖片的時候很是麻煩,因為不知道從哪裡下手,特別是面對新的開發語言,其實圖片縮放有很多種方法,但是萬變不離其宗,那就是frame,所以,不要想的那麼高大上。下面是一個利用蒙板實現的圖片縮放demo,感興趣的夥伴們可以看一下。
//
// ViewController.swift
// 圖片的縮放
//
// Created by 悅兌科技 on 15/1/15.
// Copyright (c) 2015年 BSY. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var imageview = UIImageView()
var button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
//初始化UIImageView
var imageview:UIImageView = UIImageView(frame: CGRectMake(100, 100, 100, 100))
var image = UIImage(named: "1024")
imageview.image = image
self.view.addSubview(imageview)
self.imageview = imageview
//初始化UIButton(蒙板)
var button:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
var frame = imageview.frame
button.frame = frame
self.view.addSubview(button)
button.addTarget(self, action: "changeImage:", forControlEvents: UIControlEvents.TouchUpInside)
self.button = button
self.button.selected = false
}
//初始化UIButton方法實現
func changeImage(sender:UIButton){
[UIView .beginAnimations("", context: nil)];
[UIView .setAnimationDelay(0.1)];
[UIView .setAnimationDuration(0.5)];
self.button.selected = !sender.selected
if(self.button.selected == true){
self.imageview.frame = UIScreen.mainScreen().bounds
}else if(self.button.selected == false){
self.imageview.frame = CGRectMake(100, 100, 100, 100)
[UIView.commitAnimations];}}
}