UIView Rotation using swift issue - ios

I would like to rotate UIView which defined on the storyboard I used
CGAffineTransform(rotationAngle: self.degreesToRadians(5))
which degreesToradians a function to convert to Radians, the rotation is working perfectly but the only problem is the UIView not vectorial (the edge of the view is not rendered properly and it looks like a saw) like in the screenshot below :

I have had the same problems as you and to solve it I just add a border to the view which is transparent, do it like this:
customView.layer.borderWidth = 1
customView.layer.borderColor = UIColor.clear.cgColor // important that it is clear
customView.layer.shouldRasterize = true
customView.layer.rasterizationScale = UIScreen.main.scale
Add that code after your rotation code.
With the code:
Without the code:

Related

Draw circle using CAShapelayer issue

I'm drawing many circles using UIBezierPath and CAShapeLayer the issue is the edge of the circles are not rendered properly and it looks like a saw like in the screenshot below :
I tried to fix the problem by forcing the performance with this snippet but did not work:
shapeLayer.shouldRasterize = true
shapeLayer.rasterizationScale = UIScreen.main.scale
Thanks to #PunetSharma in fact i had only to increase the scale for retina devices as explained in this link to be:
shapeLayer.rasterizationScale = 2.0 * UIScreen.main.scale

Is it possible to rotate an image in Storyboard or do you have to do it programmatically?

Assume you have a triangle image you want to use at different angles (e.g., 180 degrees, 90 degrees).
Is it possible to rotate the triangle image within Storyboard, or do you need to do it programmatically?
You could probably create an IBDesignable & IBInspectable UIView subclass that had a rotation angle property, and applied a transform to the image it contained.
IBInspectable allows you to expose custom properties of your custom views in IB's Attributes inspector.
Making a view IBDesignable allows you to view a preview of your custom view object in IB.
Edit:
This is a very old thread, but I decided to implement a custom UIView that allows rotation, as I described. Since it's now 2021, I used Swift:
#IBDesignable class RotatableView: UIView {
#objc #IBInspectable var rotationDegrees: Float = 0 {
didSet {
print("Setting angle to \(rotationDegrees)")
let angle = NSNumber(value: rotationDegrees / 180.0 * Float.pi)
layer.setValue(angle, forKeyPath: "transform.rotation.z")
}
}
}
That yields the following in Interface Builder:
It is possible to set layer.transform.rotation.z in User Defined Runtime Attributes. Check this answer: https://stackoverflow.com/a/32150954/2650588
YES. You can this only in the Storyboard (Interface Builder) cause it's layer related properties.
Just click on your view, go to User Defined Runtime Attributes and add the key path like so:
NOTICE:
The value is in radians. For instance, to rotate 90 degrees put 1.57.
You will only see the changes during runtime.
Programmatically some thing like this can help:
//rotate rect
myImageView.transform = CGAffineTransformMakeRotation(M_PI_2); //90 degree//rotation in radians
//For 180 degree use M_PI
Or make a macro like this:
#define DEGREES_TO_RADIANS(degree) (M_PI * (degree) / 180.0)
and use this way:
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90));//here may be anything you want 45/90/180/270 etc.
More here : apple link

Setting UIView.transform to arbitrary translate CGAffineTransform does nothing

I have a UIView called container that I want to move (offset) using affine transfrom. This view contains UIImageView and is a subview of UICollectionViewCell.
So it should be simple:
container.transform = CGAffineTransformMakeTranslation(100, 200) //render container 100 points right and 200 points down
Instead it is very hard, because theat code does not do anything. The view is rendered excatly on the same place as if I delete that line. So I added 'print' to verify what affine translation was set:
container.transform = CGAffineTransformMakeTranslation(100, 200)
print(container.transform) //prints: CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: 100.0, ty: 200.0)
That seems all right. So I tried rotating the container view instead with CGAffineTransformMakeRotation and it rotates the view just not around its center as it should according to documentation. I tried different combinations of translate, rotation and scale transforms just to find that the affine transformation matrixes set are OK, but attributes tx and ty seems to be ignored and a, b, c and d seems to be using different anchor point then the centre of the view (cannot say what that point is).
Any ideas on what can be causing this and how to fix it?
There must be something like auto layout messing things up for you. In the absence of outside influence, setting a view's affine transform to CGAffineTransformMakeTranslation(100, 200) will shift it right 100 points and down 200. I verified this by making a new Single View Project in Xcode and changing the viewDidLoad method in the ViewController.swift class to:
override func viewDidLoad()
{
super.viewDidLoad()
view.backgroundColor = UIColor.blueColor();
let container = UIView(frame: CGRectMake(0,0,100,100));
container.backgroundColor = UIColor.greenColor();
container.transform = CGAffineTransformMakeTranslation(100, 200);
view.addSubview(container);
}
As expected this makes the green container view appear 100 points to the right and 200 points down, even though its frame is (0,0,100,100).
So please check for auto layout and other such things that might influence the placement of this view, and if you can't find anything please post more code. Also, if your container view doesn't have a background color, please give it one so that you can see its position directly, instead of deducing its position by looking at the image view.
n.b. Setting a view's transform doesn't actually move the view itself, it just changes how/where it draws its content.

Trying to make UIImageView a circle and it appears twice

I am trying to add an imageview as a circle in swift to a uiview. I call the following code in swift however it creates the image to appear briefly incorrectly before it is corrected to a circle
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
theirPicMaskFour.layer.cornerRadius = theirPicMaskFour.frame.height / 2
theirPicMaskFour.layer.masksToBounds = false
theirPicMaskFour.layer.borderWidth = 0;
theirPicMaskFour.clipsToBounds = true
}
The following is the way it looks initially.
The following is how it looks after.
Try
theirPicMaskFour.layer.cornerRadius = theirPicMaskFour.frame.size.width / 2
instead of
theirPicMaskFour.layer.cornerRadius = theirPicMaskFour.frame.height / 2
Try theirPicMaskFour.layer.masksToBounds = true and log out the height before u assign just to be sure that the height isn't something out of ordinary
[EDIT]
Alloc and init it in viewDidLoad but update the round rect in viewDidLayoutSubviews as it will be called multiple times, but changes on frames wont effect it as that will remain same.

SWIFT rotating an image in an UIImageview defined in the Main.storyboard

I'm new to SWIFT and I'm practicing to learn but I'm having some difficulty with something.
I have an image in an UIImageview defined at the Main.storyboard that I need to rotate.
I have an IBOutlet defined as:
#IBOutlet weak var imageToRotate: UIImageView!
I was trying to rotate it with the center of rotation on the rightmost side and middle of height and as per the apple documentation and some posts in here I used the anchorPoint:
imageToRotate.layer.anchorPoint = CGPointMake( 1.0, 0.5 )
Then I try to rotate it using the transform:
imageToRotate.transform = CGAffineTransformMakeRotation(CGFloat( myRadians ) )
The image is defined in the Main.storyboard and it looks good when it loads but when I issue the anchorPoint the image gets shifted to the left and the right edge goes to where the center of the image used to be and the rotation happens around that point.
I tried using:
imageToRotate.center = CGPointMake( 487, 160)
to move the image back to the place where it's supposed to be but it doesn't move so that also has me confused.
I think I'm missing something here but I fail to figure out what it is. Do I have to define a sublayer or something else?
Also the fact that just issuing the anchorpoint moves the image tells me there's something I'm missing.
In the class definition I used this and got it linked to the storyboard's UIImage:
#IBOutlet weak var imageToRotate: UIImageView!
inside the override of viewDidLoad() I used this:
imageToRotate.layer.anchorPoint = CGPointMake(1.0, 0.5)
imageToRotate.transform = CGAffineTransformMakeRotation(CGFloat( myRadians ) )
Thanks.
All you really need to do to rate around a point is combine a translation and a rotation. The translation is an x and y coordinate with the origin at the center of the image. The rotation is a CGFloat in radians. It may look something like this:
let square2 = UIView(frame: CGRectMake(200, 200, 50, 50))
view.addSubview(square2)
// Desired point from the center to rotate around
let x = CGFloat(25)
let y = CGFloat(0)
var transform = CGAffineTransformMakeTranslation(x, y)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_4))
transform = CGAffineTransformTranslate(transform,-x,-y)
square2.backgroundColor = UIColor.magentaColor()
square2.transform = transform
This creates the following rotation around the new axis of rotation:

Resources