Ich habe ein iOS UIView mit UIViewAnimationTransitionFlipFromRight
. Ich brauche es, um vertikal zu spiegeln. Der Seitenumbruch-Übergang schneidet ihn nicht ab. Ich gehe davon aus, dass dies etwas Brauches erfordert.
Irgendwelche Ideen?
Ab iOS 5.0 ist es nicht mehr erforderlich, eine eigene Core Animation-Umwandlung zu schreiben, um vertikale Spiegelungen durchzuführen. Verwenden Sie einfach die UIKit-Übergänge UIViewAnimationOptionTransitionFlipFromTop
und UIViewAnimationOptionTransitionFlipFromBottom
, und das alles wird zu einem einzigen Methodenaufruf.
Diese verhalten sich analog zu UIViewAnimationOptionTransitionFlipFromLeft
und UIViewAnimationOptionTransitionFlipFromRight
(die es seit iOS 2.0 gibt).
Anwendungsbeispiel:
[UIView transitionFromView:viewToReplace
toView:replacementView
duration:1
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:nil];
Der obige Code kehrt die Übersicht von viewToReplace
vertikal um. Auf halbem Weg in der Animation, in dem Moment, in dem die Übersicht senkrecht zum Bildschirm und somit unsichtbar ist, wird viewToReplace
durch replacementView
ersetzt.
So einfach ist das.
Schreiben Sie einfach Ihre eigene Methode für den Flip mit Core Animation Transforms.
- (void)verticalFlip{
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0);
} completion:^{
// code to be executed when flip is completed
}];
}
Stellen Sie sicher, dass Sie die Core Animation-Bibliotheken/Frameworks hinzugefügt und eingeschlossen haben und auch math.h
Enthalten haben, wenn Sie die M_PI
- Notation verwenden möchten.
EDIT:
Wenn Sie möchten, dass es die Ansicht im Wesentlichen "ändert", wenn es halb umgedreht ist, gehen Sie folgendermaßen vor:
- (void)verticalFlip{
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI_2,1.0,0.0,0.0); //flip halfway
} completion:^{
while ([yourView.subviews count] > 0)
[[yourView.subviews lastObject] removeFromSuperview]; // remove all subviews
// Add your new views here
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0); //finish the flip
} completion:^{
// Flip completion code here
}];
}];
}
Das könnte auch funktionieren:
- (void)verticalFlip{
// Do the first half of the flip
[UIView animateWithDuration:someDuration delay:someDelay animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI_2,1.0,0.0,0.0); //flip halfway
} completion:^{
while ([yourView.subviews count] > 0)
[[yourView.subviews lastObject] removeFromSuperview]; // remove all subviews
// Add your new views here
}];
// After a delay equal to the duration+delay of the first half of the flip, complete the flip
[UIView animateWithDuration:someDuration delay:durationOfFirstAnimation animations:^{
yourView.layer.transform = CATransform3DMakeRotation(M_PI,1.0,0.0,0.0); //finish the flip
} completion:^{
// Flip completion code here
}];
}
Prost.
Der Code von Brenton hat bei mir nicht funktioniert, also habe ich ein bisschen mehr in den Apple docs und habe diesen Teil des Codes gefunden nachgeschlagen:
- (IBAction)toggleMainViews:(id)sender {
[UIView transitionFromView:(displayingPrimary ? primaryView : secondaryView)
toView:(displayingPrimary ? secondaryView : primaryView)
duration:1.0
options:(displayingPrimary ?
UIViewAnimationOptionTransitionFlipFromRight :
UIViewAnimationOptionTransitionFlipFromLeft)
completion:^(BOOL finished) {
if (finished) {
displayingPrimary = !displayingPrimary;
}
}
];
}
Sie können eine vertikale Umkehrung durchführen, indem Sie die Optionen von UIViewAnimationOptionTransitionFlipFromRight : UIViewAnimationOptionTransitionFlipFromLeft
bis UIViewAnimationOptionTransitionFlipFromTop : UIViewAnimationOptionTransitionFlipFromBottom
.
Lief wie am Schnürchen.
UIViewAnimationOptionTransitionFlipFromTop ist einfach zu verwenden, aber mit UIViewAnimationOptionTransitionFlipFromTop kann kein interaktiver Übergang erstellt werden. Wir benötigen die Transformation einer Änderungsschicht, um einen interaktiven Übergang zu erstellen.
Nur eine Transformation mit CATransform3DMakeRotation zu erstellen ist nicht genug, kein Lichteffekt, keine Perspektive. Ich schreibe ein Beispiel, um diesen Effekt hinzuzufügen. Sie können es leicht in einen interaktiven Übergang ändern.
Demo:
Beispielcode:
CALayer *sideALayer = sideAView.layer;
CALayer *sideBLayer = sideBView.layer;
CALayer *containerLayer = containerView.layer;
sideALayer.opacity = 1;
sideBLayer.opacity = 0;
sideBLayer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
containerLayer.transform = CATransform3DIdentity;
CATransform3D perspectiveTransform = CATransform3DIdentity;
perspectiveTransform.m34 = -1.0 / containerViewWidth;
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5 animations:^{
sideALayer.opacity = 0;
containerLayer.transform = CATransform3DConcat(perspectiveTransform,CATransform3DMakeRotation(M_PI_2, 0, 1, 0));
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
sideBLayer.opacity = 1;
containerLayer.transform = CATransform3DConcat(perspectiveTransform, CATransform3DMakeRotation(M_PI, 0, 1, 0));
}];
} completion:nil];
sideAView und sideBView sind Unteransichten von containerView.
Die containerView wird schwarz hinterlegt.
Swift 4.0 Version 100% Arbeitslösung
// view1: represents view which should be hidden and from which we are starting
// view2: represents view which is second view or behind of view1
// isReverse: default if false, but if we need reverse animation we pass true and it
// will Flip from Left
func flipTransition (with view1: UIView, view2: UIView, isReverse: Bool = false) {
var transitionOptions = UIViewAnimationOptions()
transitionOptions = isReverse ? [.transitionFlipFromLeft] : [.transitionFlipFromRight] // options for transition
// animation durations are equal so while first will finish, second will start
// below example could be done also using completion block.
UIView.transition(with: view1, duration: 1.5, options: transitionOptions, animations: {
view1.isHidden = true
})
UIView.transition(with: view2, duration: 1.5, options: transitionOptions, animations: {
view2.isHidden = false
})
}
Aufruf der Funktion:
anim.flipTransition(with: viewOne, view2: viewTwo)
anim.flipTransition(with: viewTwo, view2: viewOne, isReverse: true)
Es wird empfohlen, eine UIView
-Erweiterung zu erstellen und diese Funktion für diese Erweiterung zu speichern, damit auf jedes UIView
-Unterobjekt zugegriffen werden kann. Diese Lösung kann auch mit completionBlock geschrieben werden.
m in UIView zu blättern:
-(void) goToSeconVC
{
SecondVC *secondVCObj = [[SecondVC alloc] init];
[UIView beginAnimation: @”View Flip” context: nil];
[UIView setAnimationDuration: 1.0];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: self.navigationController.view cache: NO];
[sef.navigationController pushViewController: seconVCObj animated: YES];
[UIView commitAnimation];
}
**To flip into a view controller:**
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:viewController animated:YES completion:nil];
**To flip out of it:**
[self dismissViewControllerAnimated:YES completion:nil];