Ios UIview Flip使视图变暗
我在iOS中实现了一个简单的FlipView:一个包含两个子视图的UIView,一次显示一个子视图,当你点击它时,它会翻转它们。 我使用下面的动画来设置翻转的动画Ios UIview Flip使视图变暗,ios,uiviewanimation,flip,uiviewanimationtransition,Ios,Uiviewanimation,Flip,Uiviewanimationtransition,我在iOS中实现了一个简单的FlipView:一个包含两个子视图的UIView,一次显示一个子视图,当你点击它时,它会翻转它们。 我使用下面的动画来设置翻转的动画 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ @synchronized(self){ if(!self.flipping){ self.flipping = YES; UIVi
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
@synchronized(self){
if(!self.flipping){
self.flipping = YES;
UIView *toView = self.currentView == self.primaryView ? self.secondaryView : self.primaryView;
[UIView transitionFromView:self.currentView toView:toView duration:self.speed options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionCurveEaseInOut completion:^(BOOL finished) {
[self.currentView removeFromSuperview];
self.currentView = toView;
self.flipping = NO;
}];
}
}
}
很直接,对吧
但让我不安的是,当视图翻转时,翻转的内容变暗了。这表明,在光背景下
有没有人知道一个解决方案可以有完全相同的动画,但没有变暗(我最近遇到了一个类似症状的问题,每当我执行某个操作时,我都会在代码的其他地方一次又一次地添加子视图。也许你正在做类似的事情?当触摸结束时,你正在对翻转的内容做其他事情吗?如果是这样,你可能需要删除正在添加的子视图你的问题我成功了,从我在这里找到的代码中获得了灵感(他自己也从中获得了灵感) 无论如何,我所做的是在两个视图之间旋转
- (void)flip{
@synchronized(self){
if(!self.flipping){
self.flipping = YES;
UIView *bottomView = self.currentView == self.primaryView ? self.secondaryView : self.primaryView;
CALayer *top = self.currentView.layer;
CALayer *bot = bottomView.layer;
CAAnimation *topAnimation = [self flipAnimationWithDuration:self.speed/2.0 forLayerBeginningOnTop:YES scaleFactor:1];
CAAnimation *bottomAnimation = [self flipAnimationWithDuration:self.speed/2.0 forLayerBeginningOnTop:NO scaleFactor:1];
CGFloat zDistance = 1500.0f;
CATransform3D perspective = CATransform3DIdentity;
perspective.m34 = -1. / zDistance;
top.transform = perspective;
bot.transform = perspective;
topAnimation.delegate = self;
[CATransaction setCompletionBlock:^{
[top removeAllAnimations];
[self.currentView removeFromSuperview];
self.currentView = bottomView;
[self addSubview:bottomView];
[CATransaction setCompletionBlock:^{
self.flipping = NO;
[bot removeAllAnimations];
}];
[CATransaction begin];
[bot addAnimation:bottomAnimation forKey:@"flip"];
[CATransaction commit];
}];
[CATransaction begin];
[top addAnimation:topAnimation forKey:@"flip"];
[CATransaction commit];
}
}
}
-(CAAnimation *)flipAnimationWithDuration:(NSTimeInterval)aDuration forLayerBeginningOnTop:(BOOL)beginsOnTop scaleFactor:(CGFloat)scaleFactor
{
// Rotating halfway (pi radians) around the Y axis gives the appearance of flipping
CABasicAnimation *flipAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
CGFloat startValue = beginsOnTop ? 0.0f : M_PI/2;
CGFloat endValue = beginsOnTop ? -M_PI/2 : 0.0f;
flipAnimation.fromValue = [NSNumber numberWithDouble:startValue];
flipAnimation.toValue = [NSNumber numberWithDouble:endValue];
// Shrinking the view makes it seem to move away from us, for a more natural effect
// Can also grow the view to make it move out of the screen
CABasicAnimation *shrinkAnimation = nil;
if (scaleFactor != 1.0 ) {
shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
shrinkAnimation.toValue = [NSNumber numberWithFloat:scaleFactor];
// We only have to animate the shrink in one direction, then use autoreverse to "grow"
shrinkAnimation.duration = aDuration * 0.5;
shrinkAnimation.autoreverses = YES;
}
// Combine the flipping and shrinking into one smooth animation
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.animations = [NSArray arrayWithObjects:flipAnimation, shrinkAnimation, nil];
// As the edge gets closer to us, it appears to move faster. Simulate this in 2D with an easing function
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:beginsOnTop?kCAMediaTimingFunctionEaseIn:kCAMediaTimingFunctionEaseOut];
animationGroup.duration = aDuration;
// this really means keep the state of the object at whatever the anim ends at
// if you don't do this then it reverts back to the original state (e.g. brown layer)
animationGroup.fillMode = kCAFillModeForwards;
animationGroup.removedOnCompletion = NO;
return animationGroup;
}
这两个视图分别命名为primaryView和secondaryView。您可以使用任何视图(图像视图、文本视图…Hum,这可能不是问题所在,因为我在-(void)touchSedend:(NSSet*)touchWithEvent:(UIEvent*)中共享了我在-(void)touchSedend:(NSSet*)touchWithEvent中所做的所有操作事件…只是想知道,为什么要从超级视图中删除
self.currentView
?将toView
引用到已从视图层次结构中删除的视图有什么用?如果可能没有用。我不确定。无论如何,我尝试了使用或使用了should,但它没有改变任何东西。。。