swift 添加main入口文件 略谈

来源:互联网 发布:java类的实例化过程 编辑:程序博客网 时间:2024/06/10 15:20
现在通过  xcode8 创建的swift工程是没有main文件的  因为

在AppDelegate的文件中 

@UIApplicationMain

class AppDelegate:UIResponder, UIApplicationDelegate {

这样进行了注解


那么问题来了,如果想要实现自己的main文件该怎么办呢?

import Foundation

import UIKit

class MyApplication: UIApplication {

    overridefunc sendEvent(_ event:UIEvent) {

        super.sendEvent(event)

        print(event)

    }

}


UIApplicationMain(

    CommandLine.argc,

    UnsafeMutableRawPointer(CommandLine.unsafeArgv)

        .bindMemory(

            to: UnsafeMutablePointer<Int8>.self,

            capacity: Int(CommandLine.argc)),

    NSStringFromClass(MyApplication.self),

    NSStringFromClass(AppDelegate.self)

)

我这里给出一种实现方式

注意:MyApplication的sendEvent在我点击手机屏幕的时候会执行两遍

<UITouchesEvent: 0x6180000e4f80> timestamp: 37129.3 touches: {(

    <UITouch: 0x7fdd03701170> phase: Began tap count: 1 force: 0.000 window: <UIWindow: 0x7fdd06009f10; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x600000051d90>; layer = <UIWindowLayer: 0x60000002afe0>> view: <UIView: 0x7fdd03403160; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x61800002b2c0>> location in window: {215, 247.5} previous location in window: {215, 247.5} location in view: {215, 247.5} previous location in view: {215, 247.5}

)}

<UITouchesEvent: 0x6180000e4f80> timestamp: 37130.9 touches: {(

    <UITouch: 0x7fdd03701170> phase: Ended tap count: 0 force: 0.000 window: <UIWindow: 0x7fdd06009f10; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x600000051d90>; layer = <UIWindowLayer: 0x60000002afe0>> view: <UIView: 0x7fdd03403160; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x61800002b2c0>> location in window: {215, 247.5} previous location in window: {215, 247.5} location in view: {215, 247.5} previous location in view: {215, 247.5}

)}

这是两遍打印的结果,从中可以看出这是点击开始 和 结束的两个事件
0 0
原创粉丝点击