我是新来Swift的,我来自objc ..事实上,我需要在Swift中做一个"generic“类,以便由我的项目控制器调用,该项目是用objc完成的。具体地说,我必须用"Lottie“库中的几行代码来”替换“项目的动画。我只需要在这里用obj编写我的Swift类方法。你能帮帮我吗?我创建了这个Swift类,但我在superView上崩溃了,但我不确定这就是问题所在……
这是我需要在Swift中替换的代码:
LOTAnimationView *lottie1Aux = [LOTAnimationView animationNamed:[dict valueForKey:SLIDER_MAP_IMG_KEY]];
lottie1Aux.contentMode = UIViewContentModeScaleToFill;
lottie1Aux.frame = slide.tutorialImageView.bounds;
lottie1Aux.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
lottie1Aux.loopAnimation = YES;
[slide.tutorialImageView addSubview:mySwiftClass];这是我可以用来替换和优化的类Swift ...
import Foundation
import Lottie
import UIKit
@objc
public class myClass: UIViewController {
var alertView: AnimationView!
override public func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
createView()
}
@objc public func createView() {
// Create a red view
let alertWidth: CGFloat = view.bounds.width
let alertHeight: CGFloat = view.bounds.height
let alertViewFrame: CGRect = CGRect(x: 0, y: 0, width: alertWidth, height: alertHeight)
alertView = UIView(frame: alertViewFrame) as! AnimationView
alertView.backgroundColor = UIColor.red
// Create an image view and add it to this view
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: alertWidth, height: alertHeight/2))
imageView.image = UIImage(named: "tour_merchant_pagamento")
alertView.addSubview(imageView)
//view.addSubview(alertView)
alertView = AnimationView(name: "tour_merchant_pagamento")
alertView.contentMode = UIViewContentMode.scaleToFill
alertView.loopMode = .loop
}发布于 2019-11-22 14:04:57
我发现您只是想将上面给定的Objective-C代码转换为最新的Swift。我想说的是,这比以前容易多了,你可以在乐天网站的iOS部分找到指南。
现在,让我们将您的代码转换为Swift。
首先,下载并拖动,将所有Lottie json文件导入到您的Xcode项目中的一个单独文件夹中,并按您的意愿为其命名。
1-你只需在你想要的ViewController类中为你想要分配动画的单独的UIView创建一个插座,就可以让它变得更简单。在创建outlet之前,不要忘记转到类检查器并在自定义类中选择AnimationView。
然后导入Lottie,看看下面给出的代码示例。
import UIKit
import Lottie
class yourClass: UIViewController {
@IBOutlet var yourAnimationView: AnimationView! //the outlet of animationView
override func viewDidLoad() {
createAnimationView()
}
func createAnimationView() {
let yourAnimation = AnimationView(name: "filename") //animation object
yourAnimationView.addSubview(yourAnimation)
yourAnimationView.play()
}这比你想象的要简单。
https://stackoverflow.com/questions/58982658
复制相似问题