animateWithDuration在GMSMapView委托方法中不起作用

时间:2015-08-28 22:10:03

标签: ios objective-c xcode

我正在尝试使用下面的代码为UIView制作动画,但我无法理解为什么下面的代码不起作用。

作品 - self.searchBar在所有方法中顺利动画//除了上述方法

- (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position
{
    [UIView animateWithDuration:.3
                          delay:0
                        options: UIViewAnimationCurveEaseOut
                     animations:^{

                         [self.searchBar setFrame:CGRectMake(self.searchBar.frame.origin.x, self.searchBar.frame.origin.y - self.searchBar.frame.size.height  , self.searchBar.frame.size.width, self.searchBar.frame.size.height)];
                     }
                     completion:^(BOOL finished){
                     }];
}

不起作用 - self.searchBar在没有动画的情况下跳转

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture
{
        [UIView animateWithDuration:.3
                              delay:0.0
                            options: UIViewAnimationCurveEaseOut
                         animations:^{

                             [self.searchBar setFrame:CGRectMake(self.searchBar.frame.origin.x, self.searchBar.frame.origin.y - self.searchBar.frame.size.height  , self.searchBar.frame.size.width, self.searchBar.frame.size.height)];

                         }
                         completion:^(BOOL finished){
                         }];
}

3 个答案:

答案 0 :(得分:2)

我在Google(https://code.google.com/p/gmaps-api-issues/issues/detail?id=10349)上针对此问题打开了一个错误,这是他们的回复:

  

我们的理解是发生了这种情况,因为mapView:willMove:调用是在CoreAnimation事务中进行的。我们建议的解决方法是将dispatch_async中的UIView.animateWithDuration调用包装回主线程。有点像:

     

dispatch_async(dispatch_get_main_queue(),^ {            //动画块在这里。         });

     

这有帮助吗?

将UIView.animateWithDuration块放在dispatch_async块中为我工作。

此外,这个问题与iOS layout animation ignores duration when fired on GMSMapView dragging重复,但我没有足够的代表来标记它。如果有人保持得分,还应该注意谷歌的解决方法是Vishnu在他的回答中建议的。我没有足够的代表来评论他的答案。

答案 1 :(得分:1)

使用CABasicAnimation帮助我解决这个问题。

在我的情况下,我必须移动UIView框架。

CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"movePosition"];
moveAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake([viewToMove center].x, [viewToMove center].y)];
moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake([viewToMove center].x, y)];
moveAnimation.delegate = self;
moveAnimation.duration = 1.0;
moveAnimation.speed = 3.0;

moveAnimation.cumulative = YES;
moveAnimation.repeatCount = 1.0;
moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
moveAnimation.removedOnCompletion = NO;
moveAnimation.fillMode = kCAFillModeForwards;
[viewToMove.layer addAnimation:rotationAnimation forKey:@"movePosition"];

然后在

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag

保存viewToMove

的新职位

答案 2 :(得分:0)

您可以尝试在操作主队列中添加动画:

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture
{
 [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        //Your animation code goes in here
    }];
}