简体   繁体   English

如何在NSObject类中添加动态行为:Swift

[英]How do i add dynamic behaviour in NSObject class : Swift

I'm trying to create a custom alertview with dynamic behaviour using UIDynamicAnimator in NSObject class using swift,While adding UISnapBehaviour to a view in NSObject class init method snap behaviour is not working,For instance look at the below code 我正在尝试使用swift在NSObject类中使用UIDynamicAnimator创建具有动态行为的自定义Alertview,同时在UIObject类的init方法中将UISnapBehaviour添加到视图中init方法的快照行为不起作用,例如,看下面的代码

import UIKit

class DynamicBehaviour: NSObject {

var Animator:UIDynamicAnimator!
var TargetView:UIView!
var TestView:UIView!

override init() {
    super.init()
}

init(SourceViews:UIView) {
    super.init()

    TestView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    TestView.backgroundColor = UIColor.blueColor()
    TargetView.addSubview(TestView)

    Animator = UIDynamicAnimator(referenceView: TargetView)
    let snapBehavior: UISnapBehavior = UISnapBehavior(item: TestView, snapToPoint: TargetView.center)
    Animator.addBehavior(snapBehavior)

  }
}

"TestView" is added as subview to "Target" but snap behaviour is not working. 将“ TestView”作为子视图添加到“目标”,但是捕捉行为不起作用。

I tried the same code in ObjectiveC 我在ObjectiveC中尝试了相同的代码

#import "DynamicBehaviour.h"
#import <UIKit/UIKit.h>

@interface DynamicBehaviour ()
@property(nonatomic,strong)UISnapBehavior * Snap_behaviour;
@property (nonatomic,strong)UIDynamicAnimator * Animator;


@end
@implementation DynamicBehaviour

-(instancetype)initWithSourceView:(UIView *)TargetView{
   if (self = [super init]) {
      UIView * TestView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100,     100)];
      TestView.backgroundColor = [UIColor blueColor];
      [TargetView addSubview:TestView];

      self.Animator = [[UIDynamicAnimator    alloc]initWithReferenceView:TargetView];
       self.Snap_behaviour = [[UISnapBehavior alloc]initWithItem:TestView snapToPoint:TargetView.center];
       [self.Animator addBehavior:self.Snap_behaviour];
   }
  return self;
  }


 @end

it works fine,the "TestView" snaps to the centre of TargetView.I don't know whats wrong with swift code. 它工作正常,“ TestView”捕捉到TargetView的中心。我不知道快速代码有什么问题。

Here's what I've tried: 这是我尝试过的:

  1. The dynamic effect is working fine when coded in UIViewController class,problem exist only while subclassing NSObject in swift. 在UIViewController类中编码时,动态效果很好,问题仅在迅速将NSObject子类化时存在。
  2. I have tried the other dynamic behaviours such as UIGravityBehavior the same problem exists in swift. 我尝试了其他动态行为,例如UIGravityBehavior,同样的问题也很快出现。
  3. Done the same sample work in ObjectiveC object class its working fine,I've attached the working ObjC code too. 在ObjectiveC对象类中完成了相同的示例工作,其工作正常,我也附加了工作的ObjC代码。

It seems that the problem may exist in init method or variable decleration am not sure about that However, I don't know how to fix that problem. 似乎该问题可能存在于init方法中,或者不确定变量是否存在不确定性。但是,我不知道如何解决该问题。 I've read through many articles on the internet. 我已经阅读了互联网上的许多文章。 I've also searched StackOverflow. 我还搜索了StackOverflow。 Please help. 请帮忙。

Your code in Swift is not exactly the same as Objective-C code: Objective-C code has strong reference Snap_behaviour, but Swift code has only local readonly variable. Swift中的代码与Objective-C代码并不完全相同:Objective-C代码具有强大的Snap_behaviour引用,但是Swift代码仅具有本地只读变量。 Here is the Swift equivalent: 这是Swift的等效项:

class DynamicBehaviour: NSObject {

    var Animator:UIDynamicAnimator!
    var snapBehaviour:UISnapBehavior!
    var TargetView:UIView!
    var TestView:UIView!

    override init() {
        super.init()
    }

    init(SourceViews:UIView) {
        super.init()

        TestView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        TestView.backgroundColor = UIColor.blueColor()
        TargetView.addSubview(TestView)

        Animator = UIDynamicAnimator(referenceView: TargetView)
        snapBehaviour = UISnapBehavior(item: TestView, snapToPoint: TargetView.center)
        Animator.addBehavior(snapBehaviour)

    }
}

Usually I use UIDynamicBehaviour as base class for combined behaviors like this 通常我将UIDynamicBehaviour用作此类组合行为的基类

class CombinedBehavior: UIDynamicBehavior {

    lazy var collider: UICollisionBehavior = {
        let lazilyCreatedCollider = UICollisionBehavior()
        lazilyCreatedCollider.translatesReferenceBoundsIntoBoundary = true
        lazilyCreatedCollider.collisionMode = UICollisionBehaviorMode.Everything
        return lazilyCreatedCollider
    }()

    lazy var itemBehavior: UIDynamicItemBehavior = {
        let lazilyCreatedBehavior = UIDynamicItemBehavior()
        lazilyCreatedBehavior.allowsRotation = true
        lazilyCreatedBehavior.elasticity = 0
        lazilyCreatedBehavior.resistance = 100
        lazilyCreatedBehavior.angularResistance = 100
        lazilyCreatedBehavior.friction = 100
        return lazilyCreatedBehavior
    }()

    override init() {
        super.init()
        addChildBehavior(collider)
        addChildBehavior(itemBehavior)
    }

    func addBarrier(path: UIBezierPath, named name: String) {
        collider.removeBoundaryWithIdentifier(name)
        collider.addBoundaryWithIdentifier(name, forPath: path)
    }

    func addView(view: UIView) {
        dynamicAnimator?.referenceView?.addSubview(view)
        collider.addItem(view)
        itemBehavior.addItem(view)
    }

    func removeView(view: UIView) {
        collider.removeItem(view)
        itemBehavior.removeItem(view)
        view.removeFromSuperview()
    }
}

Put this code in your controller 将此代码放在您的控制器中

lazy var animator: UIDynamicAnimator = {
    let lazilyCreatedDynamicAnimator = UIDynamicAnimator(referenceView: self.view) // or any view you needed
    // if you need delegate in your controller uncomment this
    //        lazilyCreatedDynamicAnimator.delegate = self
    return lazilyCreatedDynamicAnimator
}()

var combinedBehavior = CombinedBehavior()

override func viewDidLoad() {
    // your other code
    animator.addBehavior(combinedBehavior)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Swift中检查字典中的NSObject项目? - How do I check a NSObject item in a dictionary in Swift? 如何在同一ViewController中初始化两个NSObject实例-Swift - How Do I Initialize Two Instances of NSObject in the same ViewController - Swift 如何使用iOS的NSCoder,NSObject和Swift 3解码Double - How do I decode a Double using NSCoder, NSObject and Swift 3 for iOS 如何在NSObject类中快速引用viewcontroller? - How to take reference of viewcontroller in NSObject class in swift? 如何向 Swift 中的 NSObject Model 变量添加值? - How to add Values to NSObject Model Variables in Swift? 我想在NSObject类中添加MBProgressHUD - I want to add MBProgressHUD in NSObject class 如何在Swift的Apptimize中添加动态变量? - How do I add Dynamic Variables in Apptimize with Swift? 如何从NSObject类以编程方式添加UIButton - How to add UIButton Programmatically from a NSObject Class 如何从NSObject类添加UIActivityIndi​​cator并对其进行控制? - How to add a UIActivityIndicator from an NSObject class and control it .? 当单击 Swift 中 NSObject 内部的 tableView 中的项目时,如何在 NSObject Class 内部的 UIViews 或 UIViewController 之间正确切换? - How to switch properly between UIViews or UIViewController inside NSObject Class when clicked the item in tableView inside of NSObject in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM