How to Write a Flip Method in Swift

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



Leave a reply



Submit