OC调用Swift方法 在 Build Settings 搜索 Packaging ,设置 Defines Module 为 YES 2、新建 LottieBridge.swift 文件,自动生成桥 ProductName-Bridgi
在 Build Settings 搜索 Packaging ,设置 Defines Module 为 YES
2、新建 LottieBridge.swift 文件,自动生成桥 ProductName-Bridging-Header.h
3、在 LottieBridge.swift 中,定义Swift类继承于OC类,声明 @objcMembers 或 @objc ,实现相关方法
import UIKitimport Lottie// 所有方法/属性声明@objcMembers class MyLottieView: UIView { private let animationView = LottieAnimationView() override init(frame: CGRect) { super.init(frame: frame) self.addSubview(animationView) } override func layoutSubviews() { super.layoutSubviews() animationView.frame = self.bounds } public func setLottieFromURL(_ url: URL?) { if let url = url { LottieAnimation.loadedFrom(url: url) { [weak self] (animation) in self?.animationView.animation = animation self?.play() } } } public func play() { animationView.play() }}// 单个方法/属性声明@objc class MyLottieView: UIView { private let animationView = LottieAnimationView() override init(frame: CGRect) {...} override func layoutSubviews() {...} @objc public func setLottieFromURL(_ url: URL?) {...} @objc public func play() {...}}
在OC代码中引用 ProductName-Swift.h ,调用Swift相关方法
#import "ProductName-Swift.h"- (void)swiftTest { MyLottieView *lottieView = [[MyLottieView alloc] initWithFrame:CGRectMake(100, 100, 320, 320)]; [self.view addSubview:lottieView]; NSURL *url = [NSURL URLWithString:@"https://assets9.lottiefiles.com/packages/lf20_N0y2Nj.JSON"]; [lottieView setLottieFromURL:url];}
在 ProductName-Bridging-Header.h 中加入OC的头声明
#import #import
在Swift代码中调用OC的相关方法
private func OCTest() { let configuration = URLSessionConfiguration.default _ = AFURLSessionManager(sessionConfiguration: configuration)}
来源地址:https://blog.csdn.net/z119901214/article/details/131900040
--结束END--
本文标题: OC与Swift的相互调用
本文链接: https://www.lsjlt.com/news/408435.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0