【问题标题】:animateWithDuration:delay:options:animations:completion: not working (strangely)animateWithDuration:delay:options:animations:completion: 不工作(奇怪)
【发布时间】:2023-03-28 06:26:01
【问题描述】:

我正在尝试使用以下代码将 UIView 翻译一段距离:

CGAffineTransform transform = CGAffineTransformMakeTranslation(-100, -100);
[self.exposureHintView setTransform:transform];
[UIView animateWithDuration:2.0
                      delay:0.0
                    options:UIViewAnimationOptionBeginFromCurrentState
                 animations:^{
                     NSLog(@"Begin!");
                     CGAffineTransform newTrans = CGAffineTransformMakeTranslation(0, 0);
                     [self.exposureHintView setTransform:newTrans];
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"End %d", finished);
                 }];

所以基本上,我只是试图将视图从某个点(例如 (-100, -100))移动到相对于自身应该位于 (0, 0) 的位置。

因为我想在视图出现后对其进行动画处理,所以我将代码放在viewDidAppear: 中。但是当我运行代码时,什么也没发生。

这里的self.exposureHintViewUIView的自定义子类,有关系吗?

为什么?

【问题讨论】:

  • 我发现这在模拟器上很不稳定 - 我建议在真实设备上进行测试。
  • 我正在使用真实设备...如何解决这个问题?
  • 发布你的代码曝光提示视图

标签: ios objective-c uiview core-animation


【解决方案1】:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[_AboutView setFrame:CGRectMake(0, 0, 320, 510)];
[UIView commitAnimations];

使用此代码更改视图的位置

【讨论】:

  • 谢谢,但我不知道为什么转换不起作用?
【解决方案2】:

我认为这里的问题是您以编程方式在没有代码的情况下在情节提要或 XIB 文件中创建 exposureHintView。

尝试如下操作:

- (void)viewDidLoad
{
    //Init exposureHintView
    UIView* exposureHintView = [[UIView alloc]initWithFrame(sampleFrame)];
    [self addSubView: exposureHintView];

    //Set transform
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-100, -100);
    [self.exposureHintView setTransform:transform];

    //Commit animation
    [UIView animateWithDuration:2.0 delay:0.0
                     options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
          NSLog(@"Begin!");
          CGAffineTransform newTrans = CGAffineTransformMakeTranslation(0, 0);
          [self.exposureHintView setTransform:newTrans];
    }
    completion:^(BOOL finished) {
        NSLog(@"End %d", finished);
    }];

这里的原因是当您使用情节提要或 XIB 时,您无法在 >>ViewDidLoad 中设置 Frame 或 Transform 或此 UIView 的其他属性

【讨论】:

    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2013-03-23
    相关资源
    最近更新 更多