Swift

swift-手势简单使用

2017-03-30  本文已影响261人  Coder东
//
//  ViewController.swift
//  Swift-practice002
//
//  Created by 品德信息 on 2016/12/12.
//  Copyright © 2016年 品德信息. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //显示风火轮
    UIApplication.shared.isNetworkActivityIndicatorVisible = true;
    //读取配置信息
    let mainBundle = Bundle.main
    let identifier = mainBundle.bundleIdentifier
    let info = mainBundle.infoDictionary
    let bundleId = mainBundle.object(forInfoDictionaryKey: "CFBundleName")
    let version = mainBundle.object(forInfoDictionaryKey:"CFBundleShortVersionString")
//        print("identifier\(identifier)")
//        print("info\(info)")
//        print("bundleId\(bundleId)")
//        print("version\(version)")
        //第一张xcode的基本使用已结束
    //视图的基本使用
    let rect1 = CGRect(x:30,y:50,width:200,height:200)
    let view1 = UIView(frame:rect1)
    view1.backgroundColor = UIColor.red
    
    let rect2 = CGRect(x:90,y:120,width:200,height:200)
    let view2 = UIView(frame:rect2)
    view2.backgroundColor = UIColor.purple
    
//        self.view.addSubview(view2)
//        self.view.addSubview(view1)
    
//        let view3 = UIView(frame:CGRect(x:20,y:80,width:280,height:280))
//        view3.backgroundColor = UIColor.red
//        self.view.addSubview(view3)
  //        
//        let view4 = UIView(frame:CGRect(x:0,y:0,width:200,height:200))
    //        view4.bounds = CGRect(x:-40,y:-20,width:200,height:200)//它会影响子视图的位置和显示,,其中x坐标为左+右- ,y坐标为:上+下-
//        view4.backgroundColor = UIColor.yellow
//        self.view.addSubview(view4)
//        
//        
//        let viewSub = UIView(frame:CGRect(x:0,y:0,width:100,height:100))
//        viewSub.backgroundColor = UIColor.blue
//        view4.addSubview(viewSub)
//        
    
//        self.ViewLearn()
//        self.learnImage()
//        self.GradientLayer()
//        self.addImage()
//        self.transform()
//        self.tapGestureLearn()
    self.longPressLearn()
    self.doubleTap()
}
  func ViewLearn (){
    let rect = CGRect(x:30,y:50,width:200,height:200)
    let view = UIView(frame:rect)
    view.backgroundColor = UIColor.brown
    self.view.addSubview(view)
    
    let btAdd = UIButton(frame:CGRect(x:30,y:350,width:80,height:30))
    btAdd.backgroundColor = UIColor.gray
    btAdd.setTitle("Add", for: UIControlState())
    btAdd.addTarget(self, action: #selector(ViewController.addView(_:)), for: UIControlEvents.touchUpInside)
    self.view.addSubview(btAdd)
    
    let btBack = UIButton(frame: CGRect(x:120,y:350,width:80,height:30))
    btBack.backgroundColor = UIColor.gray
    btBack.setTitle("Switch", for: UIControlState())
    btBack.addTarget(self, action: #selector(ViewController.bringViewBack(_:)), for: UIControlEvents.touchUpInside)
    self.view.addSubview(btBack)
    
    let btRemove = UIButton(frame: CGRect(x:210,y:350,width:80,height:30))
    btRemove.backgroundColor = UIColor.gray
    btRemove.setTitle("Remove", for: UIControlState())
    btRemove.addTarget(self, action: #selector(ViewController.removeView(_:)), for: UIControlEvents.touchUpInside)
    self.view.addSubview(btRemove)

    
    let replyBtn = UIButton(frame:CGRect(x:210,y:440,width:80,height:30))
    replyBtn.setTitle("ReplyBtn", for: UIControlState())
    replyBtn.addTarget(self, action: #selector(ViewController.replyView(_:)), for: UIControlEvents.touchUpInside)
    self.view.addSubview(replyBtn)
    
}

func replyView(_ sender:UIButton!) {
    
}

func addView(_ sender:UIButton!) {
    let rect = CGRect(x:60,y:90,width:200,height:200)
    let view = UIView(frame:rect)
    view.backgroundColor = UIColor.purple
    view.tag = 1
    self.view.addSubview(view)
}
func bringViewBack(_ sender:UIButton!) {
    let view = self.view.viewWithTag(1)
    self.view.sendSubview(toBack: view!)
    
}
func removeView(_ sender:UIButton!) {
    let view = self.view.viewWithTag(1)
    view?.removeFromSuperview()
    
}

func learnImage() {
    let image = UIImage(named:"001")
    let imageView = UIImageView(image:image)
    imageView.frame = CGRect(x:24,y:80,width:272,height:410)
//        imageView.layer.borderWidth = 10
//        imageView.layer.borderColor = UIColor.lightGray.cgColor
    imageView.layer.cornerRadius = 150
    imageView.layer.masksToBounds = true
    
    imageView.layer.shadowColor = UIColor.black.cgColor
    imageView.layer.shadowOffset = CGSize(width:10,height:10)
    imageView.layer.shadowOpacity = 0.45
    
    self.view.addSubview(imageView)
}

func GradientLayer()  {
    let rect = CGRect(x:30,y:60,width:200,height:200)
    let gradientView = UIView(frame:rect)
    
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = gradientView.frame
    
    let fromColor   = UIColor.yellow.cgColor
    let midColor    = UIColor.red.cgColor
    let toColor     = UIColor.purple.cgColor
    gradientLayer.colors = [fromColor,midColor,toColor]
    
    view.layer.addSublayer(gradientLayer)
    self.view.addSubview(gradientView)
}

func addImage()  {
    let image = UIImage(named:"001")
    let patternColor = UIColor.init(patternImage:image!)
    self.view.backgroundColor = patternColor
}

func transform() {
    let rect  = CGRect(x:50,y:150,width:200,height:50)
    let view = UIView(frame:rect)
    view.backgroundColor = UIColor.brown
    self.view.addSubview(view)
    
    var transform = view.transform
    transform  = transform.rotated(by: 3.14/4)
    view.transform = transform;
}

func tapGestureLearn() {
    let rect = CGRect(x:32,y:80,width:2556,height:256)
    let imageView = UIImageView(frame:rect)
    
    let image = UIImage(named:"001")
    imageView.image = image
    imageView.isUserInteractionEnabled = true
    self.view.addSubview(imageView)
    
    let gesture = UITapGestureRecognizer(target:self ,action: #selector(ViewController.singleTap))
    imageView.addGestureRecognizer(gesture)
    
    
}
func singleTap() {
    let alertView = UIAlertController(title:"Information",message:"Single Tap",preferredStyle:UIAlertControllerStyle.alert)
    let OKAction = UIAlertAction(title:"OK",style:UIAlertActionStyle.default,handler:{_ in })
    alertView.addAction(OKAction)
    self.present (alertView,animated: true,completion: nil)
}
func longPressLearn() {
    let rect = CGRect(x:40,y:80,width:100,height:100)
    let imageView = UIImageView(frame:rect)
    let image = UIImage(named:"001")
    imageView.image = image
    imageView.isUserInteractionEnabled = true
    self.view.addSubview(imageView)
//        (target: Any?, action: Selector?)
    let gesture = UILongPressGestureRecognizer(target:self ,action: #selector(ViewController.longPress(gesture:)))
    imageView.addGestureRecognizer(gesture)
}
func longPress (gesture:UILongPressGestureRecognizer) {
    
    if (gesture.state ==  UIGestureRecognizerState.began)
    {
        let alertView = UIAlertController(title:"信息",message:"长按手势",preferredStyle:UIAlertControllerStyle.alert)
        let OKAction = UIAlertAction(title:"OK",style:UIAlertActionStyle.default,handler:{_ in  })
        let  CancelAction = UIAlertAction(title:"Cancel", style: UIAlertActionStyle.default, handler: {_ in })
        alertView.addAction(OKAction)
        alertView.addAction(CancelAction)
        self.present(alertView,animated:  true ,completion: nil)
        
    }
}
func doubleTap() {
    let rect = CGRect(x:40,y:280,width:100,height:100)
    let imageView = UIImageView(frame:rect)
    let image = UIImage(named:"001")
    imageView.image = image
    imageView.isUserInteractionEnabled = true
    self.view.addSubview(imageView)
    //        (target: Any?, action: Selector?)
    let gesture = UITapGestureRecognizer(target:self ,action: #selector(ViewController.doubleTap1))
    gesture.numberOfTapsRequired = 2
    gesture.numberOfTouchesRequired = 1
    imageView.addGestureRecognizer(gesture)


}
func doubleTap1() {
     let alertView = UIAlertController(title:"信息",message:"点击两次",preferredStyle:UIAlertControllerStyle.alert)
    let OKAction = UIAlertAction(title:"OK",style:UIAlertActionStyle.default,handler:{_ in })
    alertView.addAction(OKAction)
    self.present (alertView,animated: true,completion: nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
上一篇下一篇

猜你喜欢

热点阅读