【问题标题】:Get call back when frame change换帧时回调
【发布时间】:2023-03-25 17:21:02
【问题描述】:

我正在尝试使用动画更改视图的框架。我想知道动画期间的帧。这是我正在做的事情:

[_viewTemp addObserver:self forKeyPath:@"frame" options:0 context:NULL];
[UIView animateWithDuration:1.0 animations:^{
    CGRect frame = _viewTemp.frame;
    frame.origin.x += 100;
    _viewTemp.frame = frame;
}];

这里_viewTemp 是我的 UIView 类对象。我希望在动画工作时改变每一帧。 喜欢: 当前原点是 {10,10} 动画完成后它将是 {110,10}。我想要为每个帧更改(如 {11,10}、{12、10})提供回调。

我不知道这是否可能。使用 KVO,我只能回电一次。即使通过创建子类和处理

- (void)setFrame:(CGRect)frame;

没有按预期工作。

只是想确定这是否可能,如果是,而不是如何。

谢谢。

【问题讨论】:

    标签: iphone ios objective-c uiview frame


    【解决方案1】:

    您将不得不使用计时器。在计时器事件处理程序中执行此操作

    -(void)timerHandler:(NSTimer *)timer {
    
      CGRect frame = [_tempView.layer.presentationLayer frame]; 
    
    }
    

    【讨论】:

      【解决方案2】:

      通过使用:

      #define kRight          YES
      #define kLeft           NO;
      
      @interface ViewController ()
      {
          bool _moveDirection;
      }
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
          _moveDirection = kRight;
      }
      
      - (IBAction)actionChange:(id)sender {
      
          __block NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:-1 target:self selector:@selector(handleAnimationTimer:) userInfo:nil repeats:YES];
      
          [UIView animateWithDuration:1.0 animations:^{
      
              CGRect frame = self.outletLabel.frame;
      
              if(_moveDirection == kRight) {
                  _moveDirection = kLeft;
                  frame.origin.x += 100;
              } else {
                  _moveDirection = kRight;
                  frame.origin.x -= 100;
              }
              self.outletLabel.frame = frame;
      
          } completion:^(BOOL finished) {
      
              // Remove timer
              [timer invalidate];
              timer = nil;
          }];
      
      }
      
      
      -(void)handleAnimationTimer:(NSTimer *)timer{
      
          CGRect frame = self.outletLabel.frame;
          NSLog(@"frame X %.00f", frame.origin.x);
      
      }
      

      您将能够看到 Frame 改变动画中 - 它仅在 开始时设置 的动画。

      我建议您滚动自己的帧动画(通过使用上面显示的预定计时器)并在每次调用期间调整帧

      handleAnimationTimer:(NSTimer *)timer
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-01
        • 2019-11-07
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多