更新UIImageView(Stone'对象')的位置,动画不起作用

时间:2016-04-04 05:57:11

标签: ios objective-c animation uiimageview

我试图对滑动石头的动作进行编码。围绕5x5网格的图像。 简而言之,如果我的一块石头从第3栏第1行开始,我向下滑动,它应该在第3栏第4行结束(基于NSInteger数组板[5] [5])。 我在@implementation {}中有一个Outlet View实例变量,如下所示:

    IBOutlet UIView* _one;

这是我在视图中向下滑动时所发生的一切' _one'

    - (void)swipeDown:(UISwipeGestureRecognizer *)recognizer {
         NSLog(@"Swipe Down");
         UIView *view = recognizer.view;
         if (view == _one) {
             _currentCol = stoneOneColumn;
             _currentRow = stoneOneRow;
             _state = (BoardCellState)BoardCellStateStoneOne;
             NSLog(@"Got it 3");
         } else if (view == _two) { // code for '_two' }
         [self move:CGPointMake(0, 1) withView:view];
    }

    - (void)move:(CGPoint)direction withView:(UIView*)stoneView {
         NSLog(@"Got it 5");
         CGFloat oldCol = _currentCol;
         CGFloat oldRow = _currentRow;
         while ([self indexValid:_currentCol y:_currentRow]) {
             NSLog(@"Got it 5.5");
             CGFloat newCol = _currentCol + direction.x;
             CGFloat newRow = _currentRow + direction.y;
             if ([self indexValid:newCol y:newRow]) {
                 NSLog(@"Got it 5.75");
                 _currentCol = newCol;
                 _currentRow = newRow;
             } else {
                 NSLog(@"Got it close to 6");
                 _currentCol = newCol;
                 _currentRow = newRow;
                 break;
             }
         }
         NSLog(@"Got it 6");
         [_board setCellState:BoardCellStateEmpty forColumn:oldCol
                                                     andRow:oldRow];
         [_board setCellState:_state forColumn:_currentCol 
                                        andRow:_currentRow];
         NSLog(@"Got it 7");
         [UIView animateWithDuration:0.8
                               delay:1.0
                             options:UIViewAnimationOptionCurveLinear
                          animations:^{
                             CGRect rect = stoneView.frame;
                             rect.origin.x = 
                                 _frame.origin.x + (_currentCol*61.5);
                             rect.origin.y =
                                 _frame.origin.y + (_currentRow*61.5);
                          }
                          completion:^(BOOL finished) {
                             NSLog(@"Done finally");
                          }];
    }

    - (BOOL)indexValid:(NSInteger)x y:(NSInteger)y {
        NSLog(@"Got it 8");
        BOOL indexValid = TRUE;
        if ((x < 0) || (x > 4) || (y < 0) || (y > 4)) {
            NSLog(@"Got it 10");
            indexValid = FALSE;
            return indexValid;
        } else
            return indexValid;
    }

这是输出:

    2016-04-04 01:52:22.358 Twinstones[8672:907] Swipe Down
    2016-04-04 01:52:22.363 Twinstones[8672:907] Got it 3
    2016-04-04 01:52:22.365 Twinstones[8672:907] Got it 5
    2016-04-04 01:52:22.367 Twinstones[8672:907] Got it 8
    2016-04-04 01:52:22.368 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.370 Twinstones[8672:907] Got it 5.5
    2016-04-04 01:52:22.372 Twinstones[8672:907] Got it 8 
    2016-04-04 01:52:22.374 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.375 Twinstones[8672:907] Got it 5.75
    2016-04-04 01:52:22.377 Twinstones[8672:907] Got it 8
    2016-04-04 01:52:22.379 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.380 Twinstones[8672:907] Got it 5.5
    2016-04-04 01:52:22.382 Twinstones[8672:907] Got it 8
    2016-04-04 01:52:22.384 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.385 Twinstones[8672:907] Got it 5.75
    2016-04-04 01:52:22.387 Twinstones[8672:907] Got it 8
    2016-04-04 01:52:22.389 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.392 Twinstones[8672:907] Got it 5.5
    2016-04-04 01:52:22.393 Twinstones[8672:907] Got it 8
    2016-04-04 01:52:22.395 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.397 Twinstones[8672:907] Got it 5.75
    2016-04-04 01:52:22.398 Twinstones[8672:907] Got it 8
    2016-04-04 01:52:22.400 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.402 Twinstones[8672:907] Got it 5.5
    2016-04-04 01:52:22.403 Twinstones[8672:907] Got it 8
    2016-04-04 01:52:22.405 Twinstones[8672:907] Got it 9
    2016-04-04 01:52:22.407 Twinstones[8672:907] Got it 10
    2016-04-04 01:52:22.409 Twinstones[8672:907] Got it close to 6
    2016-04-04 01:52:22.411 Twinstones[8672:907] Got it 6
    2016-04-04 01:52:22.412 Twinstones[8672:907] Got it 7
    2016-04-04 01:52:22.416 Twinstones[8672:907] Done finally

以下是对正在发生的事情的一些评论:

    1. BoardCellState is an enum (...StoneOne or ...StoneTwo) signifies the
       state of the stone (is the view I'm animating one or two).
    2. The while loop runs through the numerical positions, using the 
       indexValid method to break it when either col or row positions exceed
       the _board[5][5] 'boundary'.
    3. There are setter and getter methods in another file for stone positions.
       I set the Empty state to the old column and row, and set the current
       state to the new column and row.
    4. THEN I perform the animation! But the problem is, the stone on my 
       iPhone (when I test it, that is) doesn't move at all, even though
       the output goes through this entire process.  That's where I'm stuck.

我无法说明问题所在。它是动画中的代码:&#39;块? 我是否需要更改我的返回值:withView:&#39;方法? 动画方法的[UIView ...]是否理解我想要制作动画的是“石头视图”。我在整个过程中成功通过了什么?

谢谢你看看这个 - 安东尼

1 个答案:

答案 0 :(得分:0)

我认为您忘了将rect重新分配给stoneView.frame

animations:^{
CGRect rect = stoneView.frame;
rect.origin.x = 
_frame.origin.x + (_currentCol*61.5);
rect.origin.y =
_frame.origin.y + (_currentRow*61.5);
  

stoneView.frame = rect;

}