Detect a two-finger rotation gesture (screwing motion),
clockwise or counterclockwise.
The above rotation is 45° counterclockwise,
centered in the big white
Since we are using a
(a subclass of
we do not need to turn on the
And since the gesture is being recognized by the
not by the
we do not need to turn on the
as we did in
We saw the function
property of class
To simulate two fingers, press the option key as you drag on the simulator. To keep the distance between the fingers constant, hold down the shift key too.
View: added methods
Images.xcassets, an Xcode asset catalog file.
Contents.json: a JSON file listing the files belonging to the assad image set.
assad.png: one of the files belonging to the assad image set. Apple prefers
.pngformat. 220 × 365 pixels.
on your Desktop.
To convert it from jpg to png,
open it with the Preview application.
File → Export…
See America for instructions on how to add an image file to
Viewdisplaying the current rotation in degrees. Add the following property to class
let label: UILabel = UILabel();Insert the following after the call to
addSubview. I would have preferred to pass the label’s frame as a parameter to the
init(frame:)method of the label. But I couldn’t do this because the label’s
inithas to be called before
super.init, and the label’s frame has to be created after
label.font = UIFont(name: "Courier", size: 24); //monospace let attributes: [NSObject: AnyObject] = [NSFontAttributeName: label.font]; let size: CGSize = "-12345.6°".sizeWithAttributes(attributes); //Put the label in lower right corner of this View. label.frame = CGRectMake( bounds.origin.x + bounds.size.width - size.width, bounds.origin.y + bounds.size.height - size.height, size.width, size.height); label.backgroundColor = UIColor.yellowColor(); label.textAlignment = NSTextAlignment.Right; label.autoresizingMask = UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin; addSubview(label);Insert the following after the call to
//Display an initial value of 0 degrees in the label. rotate(recognizer);Insert the following after creating the variable
//Display the degrees rounded to one digit to the right of the //decimal point. The f stands for "float". label.text = NSString(format: "%.1f°", degrees);The label’s
autoresizingMaskkeeps it jammed into the lower right corner of the
View. The distance between the right edge of the label and the right edge of the
Viewremains fixed at zero. If the
Viewchanges its width (e.g., during an orientation change), only the distance betwen the left edge of the label and the left edge of the
Viewis allowed to change.
I was hoping to use the
to keep the
centered in the
by giving the
flexible margins on all four sides.
But the combination of rotation and autoresizing squashed the photo horribly.
UIPanGestureRecognizerto this app to let the user drag the picture as well as rotate it. See the concatenation of two
CGAffineTransforms in Animate.