swift - 一个从点击位置变大的view

2017-03-14  本文已影响76人  GA_
//
//  GA_ImageView.swift
//  GA_BrowseImage
//
//  Created by houjianan on 2016/12/5.
//  Copyright © 2016年 houjianan. All rights reserved.
//

import UIKit

class GA_ImageView: UIView {
    
    typealias TouchEndedHandler = () -> ()
    var touchEndedHandler: TouchEndedHandler!
    
    lazy var myImageView: UIImageView = {
        let i = UIImageView()
        i.frame = self.bounds
        i.image = UIImage(named: "123.png")
        self.addSubview(i)
        return i
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(frame: CGRect, handler: @escaping TouchEndedHandler) {
        self.init(frame: frame)
        touchEndedHandler = handler
        print(myImageView)
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        print(touches)
        let touch = touches.first
        let view = touch?.view
        print(view!)
        let location = touch?.location(in: view)
        print(location!)
        
        let x: CGFloat = (location?.x)!
        let y: CGFloat = (location?.y)!
        
        let v = UIView(frame: CGRect(x: x, y: y, width: 0, height: 0))
        v.backgroundColor = UIColor.orange
        self.addSubview(v)
        
        let superView = self.superview!
        
        UIView.animate(withDuration: 0.4, animations: {
            v.frame = CGRect(x: -self.frame.origin.x, y: -self.frame.origin.y, width: superView.frame.size.width, height: superView.frame.size.height)
        }, completion: {
            bo in
            UIView.animate(withDuration: 0.3, animations: { 
                v.alpha = 0
            }, completion: { (bo) in
                v.removeFromSuperview()
                self.touchEndedHandler()
            })
        })
    }
}
上一篇下一篇

猜你喜欢

热点阅读