xcode老swift转新代码
2018-07-06 22:34:41 weixin_42596600 阅读数 35

import UIKit

import Foundation

class ViewController: UIViewController {

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        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)

    }

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    

}


2018-07-11 17:05:07 weixin_42596600 阅读数 125

//

//  PageControlViewController.swift

//  PageControlViewProject

//

//  Created by 董黎伟 on 2018/7/7.

//  Copyright © 2018年 董黎伟. All rights reserved.

//


import UIKit


class PageControlViewController: UIViewController, UIScrollViewDelegate{

// UIScrollViewDelegate 此为视图控制器添加滚动代理协议以及滚动视图属性

    var scrollView = UIScrollView()

    var pageControl = UIPageControl()

//    控制翻页

    var isPageControlUsed = false

//    标志滑动状态

    

    override func viewDidLoad() {

        super.viewDidLoad()

    

    var screenFrame = UIScreen.main.bounds

//    获取当前设备屏幕尺寸信息

    let screenWidth = screenFrame.size.width

    let screenHeight = screenFrame.size.height

//    获得屏幕尺寸的宽度值和高度值

    scrollView.frame = screenFrame

//    创建一个区域来显示之前创建的视图控制器

    scrollView.isPagingEnabled = true

//    设置滚动视图为分页模式,即每滚动一次就是一页

    scrollView.contentSize = CGSize(width: screenWidth * 2, height: screenHeight)

//    设置滚动视图尺寸为2倍页面宽度,因为我有2个页面

    scrollView.backgroundColor = UIColor.black

//    设置滚动视图背景色为黑色

    scrollView.delegate = self

//    设置滚动视图对象的代理为当前类。这样就可以在当前文件中编写代理方法,以捕捉滚动视图的相关动作。

    let pcHeight: CGFloat = 50.0

//    创建一个高度常量,作为页面控制器对象的高度

    let pcRect = CGRect(x: 0, y: screenHeight - pcHeight, width: screenWidth, height: pcHeight)

//    创建一个区域用来显示页面控制器对象

    pageControl.frame = pcRect

//    设置页面控制器对象的显示区域

    pageControl.numberOfPages = 2

//    设置控制器对象的总页数为两页

    pageControl.currentPage = 0

//    设置页面控制器对象的当前页编号

    pageControl.backgroundColor = UIColor.gray

//    设置控制器对象的背景颜色

    pageControl.addTarget(self, action: #selector(PageControlViewController.pageControlDidChanged(_ :)), for: UIControlEvents.valueChanged)

//    给页面控制器对象,添加监听页面切换事件的方法。

    let firstController = FirstSubViewController()

//    创建第一个视图控制器对象的实例。

    screenFrame.origin.x = 0

//    设置坐标原点的横向值为0

    firstController.view.frame = screenFrame

//    设置第一个视图控制器对象的显示区域

    let secondController = SecondSubViewController()

    screenFrame.origin.x = screenFrame.size.width

//    设置坐标原点的x值为屏幕宽度,即第二个视图控制器对象显示在屏幕之外

    secondController.view.frame = screenFrame

//    设置第二个视图控制对象的显示区域

    scrollView.addSubview(firstController.view)

    scrollView.addSubview(secondController.view)

//    将两个视图控制器的根视图,分别添加到滚动视图对象

    self.view.addSubview(scrollView)

    self.view.addSubview(pageControl)

//    把滚动视图和页面控制器对象,分别添加到当前窗口的根视图里

}

    

@objc func pageControlDidChanged(_ sender: AnyObject)

//    创建监听页面切换事件的方法

{

    let crtPage = (CGFloat)(pageControl.currentPage)

//    获得当前页面控制器对象的当前页面

    var frame = scrollView.frame

//    获得滚动视图当前的显示区域

    frame.origin.x = frame.size.width * crtPage

    frame.origin.y = 0

//    根据页面控制器对象的目标页码,计算滚动视图在下一页中的显示区域

    scrollView.scrollRectToVisible(frame, animated: true)

//    然后滚动视图到目标区域

    isPageControlUsed = true

//    设置通过 页面控制器对象切换页面

}


func  scrollViewDidScroll(_ scrollView:UIScrollView)

//创建监听滚动视图的滚动事件的代理方法

{

    if(!isPageControlUsed) {

//        如果是通过页面控制器对象切换页面,则不执行后面的代码

        let pageWidth = scrollView.frame.size.width

//        获得滚动视图的宽度值

        let page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth)+1

//        根据滚动视图的宽度值和横向位移量,计算当前页码

        pageControl.currentPage = Int(page)

//        设置页码控制器的显示页码为通过计算得到的页码

        

    }

}



func scrollViewDidEndDecelerating(_ scrollView: UIScrollView){

//    创建监听滚动视图的滚动减速事件的代理方法,重置标示变量的默认值。然后打开代理文件

    isPageControlUsed = false

}




    



override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

}


    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destinationViewController.

        // Pass the selected object to the new view controller.

    }

    */


}


2018-07-07 00:50:50 weixin_42596600 阅读数 67


import UIKit

import Foundation

class ViewController: UIViewController {

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        let rect = CGRect(x: 50, y: 100, width: 256, height: 256)

        let imageview = UIImageView(frame: rect)

        

        let image = UIImage(named: "timg")

        imageview.image = image

        

        imageview.isUserInteractionEnabled = true

        self.view.addSubview(imageview)

        let guesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress))

//        定义长按手势

        imageview.addGestureRecognizer(guesture)

    }

    

    @objc func longPress(gusture:UILongPressGestureRecognizer)

    {

        if(gusture.state == UIGestureRecognizerState.began)

        {

        let alertView = UIAlertController(title: "Infomation", message: "Long Press", preferredStyle: UIAlertControllerStyle.alert)

        let OkAction = UIAlertAction(title: "ok",style: .default, handler: {_ in})

        alertView.addAction(OkAction)

            self.present(alertView,animated: true,completion: nil)

            

        }

        

    }

//        点击图片会弹出窗口提示 Single tap

   

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    

    

}


2018-07-16 15:37:12 weixin_42596600 阅读数 258

//

//  AppDelegate.swift

//  NavigationViewControllerproject

//

//  Created by 董黎伟 on 2018/7/15.

//  Copyright © 2018年 董黎伟. All rights reserved.

//


import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        let viewController = FirstSubViewController()

//        实例化第一个视图控制器对象

        let navigationController = UINavigationController(rootViewController: viewController)

//        初始化导航视图控制器对象,并将第一个视图控制器对象,作为导航根视图控制器

        self.window?.rootViewController = navigationController

        return true

    }


    func applicationWillResignActive(_ application: UIApplication) {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

    }


    func applicationDidEnterBackground(_ application: UIApplication) {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    }


    func applicationWillEnterForeground(_ application: UIApplication) {

        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    }


    func applicationDidBecomeActive(_ application: UIApplication) {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    }


    func applicationWillTerminate(_ application: UIApplication) {

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    }



}


2018-07-11 18:15:27 weixin_42596600 阅读数 134

//

//  AppDelegate.swift

//  TabBarViewControllerProject

//

//  Created by 董黎伟 on 2018/7/11.

//  Copyright © 2018年 董黎伟. All rights reserved.

//


import UIKit


@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {


    var window: UIWindow?



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

//        在应用启动完成的代理方法中,创建程序的入口

        let viewController1 = FirstSubViewController()

//        实例化第一个视图控制器对象

        let viewController2 = SecondSubViewController()

//        实例化第二个视图控制器对象

        let tabViewController = UITabBarController()

//        初始化一个选项卡控制器对象

        tabViewController.viewControllers = [viewController1,viewController2]

//        将两个视图控制器,以数组的方法,指定给选项卡控制器对象

        tabViewController.view.backgroundColor = UIColor.red

//        设置根视图背景色

        self.window?.rootViewController = tabViewController

//        将选项卡的控制器对象,作为当前窗口的根视图控制器

        return true

    }


    func applicationWillResignActive(_ application: UIApplication) {

        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

    }


    func applicationDidEnterBackground(_ application: UIApplication) {

        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    }


    func applicationWillEnterForeground(_ application: UIApplication) {

        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    }


    func applicationDidBecomeActive(_ application: UIApplication) {

        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    }


    func applicationWillTerminate(_ application: UIApplication) {

        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    }



}


没有更多推荐了,返回首页