0

I'm new in Swift, I come from objc .. in fact I need to do a classse in Swift "generic" to be called by my controller of the project done in objc. specifically I have to "replace" the few lines of code in the "Lottie" library for the animations of the project. I need to write only my Swift class methods here in obj c .. can you help me? I created this Swift class but I have crashes on the superView, but I'm not sure that's just the problem ...

this is the code I need to replace in 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];

and the is the Class Swift that I can make for replace and optimize...

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

}
EdoBen
  • 1,676
  • 2
  • 14
  • 24
  • 1
    Please formulate your question correctly. What do you need to do? – hamsternik Nov 21 '19 at 20:20
  • @Hamsternik I need to create a swift class that will satisfy that code written in objc, so that from my objc file after I call my method in swift .. this is because I have the project in objc but I would like to have the Lottie library updated to the latest version however it no longer supports objc –  Nov 21 '19 at 20:53

1 Answers1

1

I found that you just wanted to convert the above given Objective-C code to latest Swift. I would like to say that its far more easier than before and you can find the guide in Lottie website in iOS section.

Now let's convert your code to Swift.

First download and drag import all Lottie json files to a separate folder inside your Xcode project and name it simple as you wish.

1- You can make it simpler now a days by just creating an outlet for a separate UIView that you wish to assign your animation to inside your desired ViewController class. Don't forget to go to class inspector and select AnimationView in custom class before creating outlet.

Then import Lottie and have a look at below given code example.

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() 


}

It's simple than you think.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jaseel.Dev
  • 730
  • 1
  • 7
  • 19
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/202892/discussion-on-answer-by-jaseel-dev-generic-class-lottie-swift). – Samuel Liew Nov 22 '19 at 13:19