Advertisement
SEEEEEAAAAAA10000000

Template View

Jan 14th, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.16 KB | None | 0 0
  1. import UIKit
  2.  
  3. class TemplateView: UIView {
  4.    
  5.     let picture: UIImageView
  6.     var touch: (() -> Void)?
  7.    
  8.     convenience init() {
  9.         self.init(frame: CGRect.zero)
  10.     }
  11.    
  12.     override init(frame: CGRect) {
  13.         picture = UIImageView(image: nil)
  14.         picture.contentMode = .scaleAspectFill
  15.         picture.backgroundColor = UIColor.lightGray
  16.         super.init(frame: frame)
  17.         addSubview(picture)
  18.     }
  19.    
  20.     required init?(coder aDecoder: NSCoder) {
  21.         fatalError("init(coder:) has not been implemented")
  22.     }
  23.    
  24.     /// Используем вместо Autolayout. Обязательно вызываем super.
  25.     override func layoutSubviews() {
  26.         super.layoutSubviews()
  27.         picture.frame = bounds
  28.     }
  29.    
  30.     /// Перехватываем тачи. Обязательно вызываем super.
  31.     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  32.         super.touchesBegan(touches, with: event)
  33.         touch?()
  34.     }
  35.    
  36.     override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  37.         super.touchesEnded(touches, with: event)
  38.     }
  39.    
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement