How to write a flip method in Swift?
A->B->C
is the type of a function taking one argument of type A
and returning a function B->C
(a "curried" function). The type of a function taking
two arguments is (A, B)->C
:
func flip<A, B, C>(f: (A, B)->C) -> (B, A)->C {
return { (valueB: B, valueA: A) in
return f(valueA, valueB)
}
}
let x = flip(-)(10, 5)
println(x) // -5
It can slightly be shortened to
func flip<A, B, C>(f: (A, B)->C) -> (B, A)->C {
return { (valueB, valueA) in
f(valueA, valueB)
}
}
due to automatic type inference.
As far as I know, Swift does not automatically convert functions
taking multiple arguments into curried functions, compare
Typecase regular Swift function to Curry Function.
How to flip UIImage horizontally with Swift?
Most factory methods are converted to initializers in swift. Whenever available, even if the class method is still available, they are preferred. You can use:
init(CGImage cgImage: CGImage!, scale: CGFloat, orientation: UIImageOrientation)
The usage would look like this:
var image = UIImage(CGImage: img.CGImage, scale: 1.0, orientation: .DownMirrored)
Swift 5
var image = UIImage(cgImage: img.cgImage!, scale: 1.0, orientation: .downMirrored)
How can I flip a label (get the mirror view) in Swift (Xcode 6.3)
Try this:
self.labelShowdata.transform = CGAffineTransformMakeScale(-1, 1);
Swift 5:
self.table.transform = CGAffineTransform(scaleX: -1, y: 1);
How to flip UIImage horizontally?
Objective-C
UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUpMirrored];
Swift
let flippedImage = myImage.withHorizontallyFlippedOrientation()
Flip characters separately in string
You could reverse the string and then flip it.
I.e.
ABCD -> DCBA -> the flipped image you want.
Just use the transform like you did before but reverse the string first.
How do I flip and rotate a label at the same time?
As @rmaddy has suggested, prepare two transform separately, concat them together which ends up creating a single transformation, then assign it woulf look like
let flipping = CGAffineTransform(scaleX: -1, y: 1)
let rotating = CGAffineTransform(rotationAngle: (-90 * .pi / 180.0)
let fullTransformation = flipping.concatenating(rotating)
self.timeLabel.transform = fullTransformation
For more info on the concatenating
, check the doc following
https://developer.apple.com/documentation/coregraphics/cgaffinetransform/1455996-concatenating
How to flip an individual UIView (without flipping the parent view)
this code may helps you:
put the two views you want to flip inside an unnamed view with the same size
and link the IBOutlet UIView *newView,*oldView;
to the views and put the new view on top
bool a = NO;
@implementation ViewController
- (IBAction)flip:(id)sender
{
if (a == NO) {
[UIView transitionFromView:oldView toView:newView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:NULL];
a = YES; // a = !a;
}
else {
[UIView transitionFromView:newView toView:oldView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:NULL];
a = NO; // a = !a;
}
}
Related Topics
Combining Scenekit and Spritekit in a Single Screen
How to Read a File from The Filesystem in a Swift Command Line App
Possible Bug? I Can Create Generic Instance Ignoring Constraint
Firebase Authentication: Linking Multiple Accounts in Swift
Spacing Between Sections in a Form
Realitykit Entity Synchronization Is Always Nil
Qlpreviewcontroller Showing File Then Going Blank in Swiftui
Unexpectedly Found Nil While Unwrapping an Optional Value with Avaudioplayer
Uipageviewcontroller with Previous and Next Buttons
Streaming .M3U8 Using Mpmovieplayercontroller Does Not Work
Prepareforsegue from a UIbutton in a Custom Prototype Cell
Creating Semaphore with Initial Value of 0 Make Issues with Execution
How to Cut a Hole in a Sprite Image or Texture to Show What Is Behind It Using Spritekit in Swift
Validatemenuitem or Menuwillopen Not Called for Nsmenu
Can't Update Label from Other Class in Swift
Swift-Making an Sknode Simply "Move Forward" on an Angle
Why Is a Firestore Listener Returning .Added Twice When a Single Document Is Added