首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISnapBehavior奇怪的抖动- swift

UISnapBehavior奇怪的抖动- swift
EN

Stack Overflow用户
提问于 2015-08-04 07:22:44
回答 1查看 566关注 0票数 3

我正在尝试使用UISnapBehavior来创建tweening back动画。

我正在尝试使一个正方形可拖动,然后当正方形被释放时,它会补间回到它的原始位置。每次它往回走,它似乎有一个奇怪的轨迹,而不是以正确的角度放松,它似乎在到达目的地之前以一种奇怪的半圆圈的方式减速。

提前谢谢你。

我的代码如下:

代码语言:javascript
复制
var animator:UIDynamicAnimator? = nil;
var ball : Ball!;
var ball2 : Ball!;
var snap: UISnapBehavior!
var gravity : UIGravityBehavior!;

var speed : Double! = 0;
var lastTouchPoint :  CGPoint!;
var attach : UIAttachmentBehavior! = nil;
var itemBehavior : UIDynamicItemBehavior!;
var snapping : UISnapBehavior! = nil;
var currentBall : Ball! = nil;


override init(frame: CGRect) {
    super.init(frame: frame);
    println( "-- initiated view --" );

    self.backgroundColor = UIColor.orangeColor();

    ball = Ball( frame: CGRect(x: 200, y: 200, width: 50, height: 50 ) );
    self.addSubview(ball);

    ball2 = Ball( frame: CGRect(x: 100, y: 100, width: 50, height: 50 ) );
    self.addSubview(ball2);

    setup()

    self.addGestureRecognizer( UIPanGestureRecognizer(target: self, action: "panHandler:") );

    self.attach = UIAttachmentBehavior(item: self, attachedToAnchor: CGPoint(x: 0, y: 0) );

}

func setup() {
    animator = UIDynamicAnimator(referenceView:self);

    var collision: UICollisionBehavior!
    collision = UICollisionBehavior(items: [ball, ball2])
    collision.translatesReferenceBoundsIntoBoundary = false;
    collision.collisionDelegate = self;
    animator?.addBehavior(collision)

    var dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [ball, ball2] );
    dynamicItem.allowsRotation = false;
    self.animator!.addBehavior(dynamicItem);
}

func panHandler( recognizr : UIPanGestureRecognizer ) {
    switch( recognizr.state ) {
        case  UIGestureRecognizerState.Began:

            let location = recognizr.locationInView(self);
            let view : AnyObject? =  self.hitTest(location, withEvent: nil);

            if( view is Ball ) {

                currentBall = view as! Ball;

                if(  self.attach != nil ){ self.animator!.removeBehavior(attach); self.attach = nil; }


                self.attach = UIAttachmentBehavior(item: currentBall, attachedToAnchor: location );
                self.animator!.addBehavior(self.attach);
                self.attach.damping = 1;
                self.attach.length = 1;

                let dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [currentBall] );
                dynamicItem.allowsRotation = false;

                self.animator!.addBehavior(dynamicItem);
            }
            break;

        case  UIGestureRecognizerState.Changed :

            if( currentBall != nil ) {
                let location = recognizr.locationInView(self);
                self.attach.anchorPoint = location;
            }

            break;

        default :
            println("--- snap ---");

            if( self.currentBall == nil ) {return;}

            self.animator!.removeBehavior(attach);
            self.attach = nil;

            var snap : UISnapBehavior = UISnapBehavior(item: self.currentBall, snapToPoint: self.currentBall.startingPoint );
            snap.damping = 1;

            self.animator!.addBehavior(snap);

            break;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-04-03 17:59:29

现在是2018年了,我看到了几乎相同的运动。我认为这是苹果实现中的一个错误,你和我都应该提交一个错误报告,因为这是解决这个问题的唯一方法。

在我的经验中,当damping高于1.0时,这种朝向末端的半圆运动中的抖动看起来更明显。

在下面的例子中,我的手势识别器严格固定了x轴的值,所以理论上应该不会有任何水平移动。

allowsRotation设置为false

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31798290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档