[英]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: 这是我尝试过的:
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.