美文网首页
swift - 一个从点击位置变大的view

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

作者: GA_ | 来源:发表于2017-03-14 15:24 被阅读76次
    //
    //  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()
                })
            })
        }
    }
    

    相关文章

      网友评论

          本文标题:swift - 一个从点击位置变大的view

          本文链接:https://www.haomeiwen.com/subject/edkhnttx.html