试图重置UIView的位置

时间:2014-08-14 17:00:28

标签: ios objective-c uiview

我有一个使用Tweetbot 3的图像查看器的物理特性的代码。我正在开发一个游戏,当我通过滑动“启动”我希望重新生成它的视图,此视图在屏幕之外。我尝试重置原始位置但是,当视图返回内部时,屏幕全部变形。这是代码:

  #import "ClassicaViewController.h"


@interface ClassicaViewController () <UICollisionBehaviorDelegate>
@property UIDynamicAnimator *animator;
@property UICollisionBehavior *collisionBehavior;
@property UIAttachmentBehavior *attachBehavior;
@property UIPushBehavior *pushBehavior;
@property UIPanGestureRecognizer *panGesture;
@property UITapGestureRecognizer *tapGesture;
@end

@implementation ClassicaViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                          action:@selector(handleAttachmentGesture:)];
    [self.view addGestureRecognizer:_panGesture];
    _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                          action:@selector(handleTapGesture:)];
    [self.view addGestureRecognizer:_tapGesture];

}

- (void)handleAttachmentGesture:(id)sender
{
    CGPoint p = [_panGesture locationInView:self.view];
    if (_panGesture.state == UIGestureRecognizerStateBegan) {
        CGPoint center = wordView.center;
        UIOffset offset = UIOffsetMake(p.x - center.x, p.y - center.y);
        _attachBehavior = [[UIAttachmentBehavior alloc] initWithItem:wordView
                                                    offsetFromCenter:offset
                                                    attachedToAnchor:p];
        [_animator addBehavior:_attachBehavior];
    }
    else if (_panGesture.state == UIGestureRecognizerStateChanged) {
        _attachBehavior.anchorPoint = p;
    }
    else if ((_panGesture.state == UIGestureRecognizerStateEnded ||
              _panGesture.state == UIGestureRecognizerStateCancelled) &&
             _attachBehavior) {
        [_animator removeBehavior:_attachBehavior];
        _attachBehavior = nil;

        CGPoint velocity = [_panGesture velocityInView:self.view];
        velocity = CGPointMake(velocity.x / 50, velocity.y / 50); //30
        CGFloat magnitude = (CGFloat)sqrt(pow((double)velocity.x, 2.0) + pow((double)velocity.y, 2.0));
        if (magnitude > 20) {
            _collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[wordView]];
            _collisionBehavior.collisionDelegate = self;
            CGFloat diagonal = -sqrt(pow(CGRectGetWidth(wordView.frame), 2.0) +
                                     pow(CGRectGetHeight(wordView.frame), 2.0));
            UIEdgeInsets insets = UIEdgeInsetsMake(diagonal, diagonal, diagonal, diagonal);
            [_collisionBehavior setTranslatesReferenceBoundsIntoBoundaryWithInsets:insets];
            [_animator addBehavior:_collisionBehavior];

            _pushBehavior =
            [[UIPushBehavior alloc] initWithItems:@[wordView]
                                             mode:UIPushBehaviorModeInstantaneous];
            CGPoint center = wordView.center;
            UIOffset offset = UIOffsetMake((p.x - center.x) / 2.0, (p.y - center.y) / 2.0);
            [_pushBehavior setTargetOffsetFromCenter:offset forItem:wordView];
            _pushBehavior.pushDirection = CGVectorMake(velocity.x, velocity.y);
            [_animator addBehavior:_pushBehavior];

            _panGesture.enabled = NO;
        }
        else {
            __weak __typeof(self) wself = self;
            [UIView animateWithDuration:.25
                             animations:^{
                                 wordView.center = wself.view.center;
                                 wordView.transform = CGAffineTransformIdentity;
                             }
                             completion:nil];
        }
    }
}

- (void)handleTapGesture:(id)sendr
{
    CGPoint p = [wordView convertPoint:[_tapGesture locationInView:wordView]
                               toView:self.view];
    if (!CGRectContainsPoint(wordView.frame, p)) {
        NSLog(@"tap");
    }
}

- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p
{
    [_animator removeAllBehaviors];
    _pushBehavior = nil;
    _collisionBehavior = nil;
    wordView.frame = CGRectMake(74, 256, 173, 56);

}

@end

0 个答案:

没有答案