swift-手势简单使用

//
//  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.
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,717评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,501评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,311评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,417评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,500评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,538评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,557评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,310评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,759评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,065评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,233评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,909评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,548评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,172评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,420评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,103评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,098评论 2 352

推荐阅读更多精彩内容