UISliders overlap after transforming - ios

I'm currently trying to create four vertical sliders in Objective-C. I'm able to rotate them using CGAffineTransform, but right now each slider is stacked on top of one another. How do you change the coordinates of the sliders so this doesn't happen?
Below is my code for rotating the sliders:
CGAffineTransform trans1 = CGAffineTransformMakeRotation(M_PI * 1.5);
_grainPitchSlider.transform = trans1;
CGAffineTransform trans2 = CGAffineTransformMakeRotation(M_PI * 1.5);
_grainOffsetSlider.transform = trans2;
CGAffineTransform trans3 = CGAffineTransformMakeRotation(M_PI * 1.5);
_grainDensitySlider.transform = trans3;
CGAffineTransform trans4 = CGAffineTransformMakeRotation(M_PI * 1.5);
_grainDurationSlider.transform = trans4;

Simply set the "center" of each slider, i.e. _grainPitchSlider.center = CGPointMake(x, y);

Related

How to rotate image view while not changing center X position

I want to rotate my image view using following:
CGFloat degrees = -20.0f; //the value in degrees
CGFloat radians = degrees * M_PI/180;
_arrowImgView.transform = CGAffineTransformMakeRotation(radians);
It does rotate, but unfortunatelly it does rotate whole image moving it up. Please take a look at screenshots:
On second screenshot you can see that image is moved up and changed X coordinate. How to simply bend it like clock arrow?
UPDATED:
I changed code to:
self.arrowImgView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
CGFloat degrees = -20.0f; //the value in degrees
CGFloat radians = degrees * M_PI/180;
self.arrowImgView.transform = CGAffineTransformMakeRotation(radians);
Also i want to notice that i did set constraints like that :
[_arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(self);
make.centerX.equalTo(self.mas_centerX);
}];
Try this code
imageView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
imageView.transform = CGAffineTransformMakeRotation(angle);
Reference: https://developer.apple.com/reference/quartzcore/calayer/1410817-anchorpoint
Edit
I tried this below code in swift it works here
self.viewRotate.layer.anchorPoint = CGPoint(x: 0, y: 0)
let degree : CGFloat = -20.0
let rad : CGFloat = degree*CGFloat(M_PI/180)
viewRotate.transform = CGAffineTransform(rotationAngle: rad)
Output
Before rotation
After rotation

Scaling down the width of a view after doing rotation by using CGAffineTransformScale

I have drawn a view(black rectangle) straight to the x-axis then I am applying some rotation by using CGAffineTransformMakeRotation.Please see image 1 after rotation.
After this I need to reduce the width from that rotated position.
For that I am using CGAffineTransformScale but image is not scaling down along the slope.Please see image 2.
If I change width from bound property then it works fine.
Any idea why CGAffineTransformScale behaves like that.
Below is my sample code.
**__block CGAffineTransform rotationTransform;
rotationTransform = CGAffineTransformMakeRotation(angle);
lineview.transform = rotationTransform;
[UIView animateWithDuration:10 animations:^{
CGAffineTransform finalTransform = CGAffineTransformScale(lineview.transform, 0.4, 1);
lineview.transform = finalTransform;
} completion:^(BOOL finished) {
}];
Try this to move the frame and change the width :)
VIEW.frame = CGRectMake(0,39, 320, 111);
This is the format
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)

How to rotate UIView using transform?

I have a UIView which I am trying to transform (rotate) using the pitch value you can get from coremotion attitude. I am using transform however I am not sure my code is correct as the UIView is not doing anything.
This is my code its being called several times per second.
- (void)setLabelValueRoll:(double)roll pitch:(double)pitch yaw:(double)yaw
{
self.yLabel.text = [NSString stringWithFormat:#"pitch: %f", pitch];
CATransform3D transform;
transform = CATransform3DMakeRotation(pitch + M_PI_2, 1, 0, 0);
self.wapaA.layer.sublayerTransform = transform;
}
I am not sure how to set this part (pitch + M_PI_2, 1, 0, 0) to make the pitch affect my square UIView so it would I guess stay level as you tilt the phone back and forth.
It should be layer's transform property of UIView
wapaA.layer.transform = CATransform3DMakeRotation(pitch + M_PI_2, 1, 0, 0);
This will work
float degrees = 20; //the value in degrees
view.transform = CGAffineTransformMakeRotation(degrees * M_PI/180);

while performing dual operation on UIImageView

This time I am facing problem while performing dual operation on UIImageView means
1) Zoom+Flip the Image
2)Zoom + Reverse the Image
3)Zoom + Flip or Reverse + Rotate(+- 90)deg
so please help to find out the solution of this problem. In thiscase for Zoom I use the GuestureRecognizer for fliping+Revesing+Rotating UIImageView I Use the the UIButton for these operation so how I can handle these operations wiith combination on UIImage
what exact the problem When I Zoom In or Zoom Out the image the at the same time I want to rotate (+-90 deg) or Flip or Reverse the Image at exact zoom In or Zoom Out Position but that time Image get reset then perform the respective operation such rotate, flip ,reverse I wanted to do it simultaneously on UIImage
my code as
if (btnNames == "Rotate R")
{
Angle = deg + 90;
imgview.Transform = CGAffineTransform.MakeRotation (Angle * (float)Math.PI / 180);
return Angle;
} else
{
Angle = deg - 90;
imgview.Transform = CGAffineTransform.MakeRotation (Angle * (float)Math.PI / 180);
return Angle;
}
For Reverse
CGAffineTransform transform = CGAffineTransform.MakeScale (-1, 1);
imgView.Transform = transform;
return true;
============
For Flip
if (flips == false)
{
CGAffineTransform transform = CGAffineTransform.MakeScale (1, -1);
imgView.Transform = transform;
return true;
}
this code i implemented it works fine individually perform to operation. But I want to work simultaneously on UIImage
I would recommend:
Place the UIImage in a UIScrollView with zooming setup
Modify your UIImage's Transform for reverse, rotate, etc.
To setup zooming:
yourScroller.MaximumZoomScale = 5;
yourScroller.MinimumZoomScale = 1;
yourScroller.ViewForZoomingInScrollView = _ => yourImage;
An example for rotation would be:
yourImage.Transform.Rotate (Math.PI / 2)
Play around with it, the UIScrollView should modify the same Transform on the image, so everything should work in tandem.
I'm not sure I really understand what you mean, but it sounds like your trying to do more the one transformations for an image by setting the UIView's transform property like this (example):
myView.transform = scale;
myView.transform = flip;
myView.transform = rotate;
What happens then is, that only the last one of these "is active: on the view.
What you should do is "keeping/saving" the current transformation and appending the new one with CGAffineTransformConcat like:
myView.transform = scale;
myView.transform = CGAffineTransformConcat(myView.transform, flip);
myView.transform = CGAffineTransformConcat(myView.transform,rotate);
Keep in mind that for some transformations order of concatenation matters!
Transformations can be combined by applying them to an existing transformation:
CGAffineTransform transform = CGAffineTransformIdentity;
// Rotate
if (btnNames == "Rotate R") {
Angle = deg + 90;
transform = CGAffineTransformRotate(transform, Angle * (CGFloat)Math.PI / 180);
} else {
Angle = deg - 90;
transform = CGAffineTransformRotate(transform, Angle * (CGFloat)Math.PI / 180);
}
// Reverse
transform = CGAffineTransformScale(transform, -1, 1);
// Flip
if (!flips) {
transform = CGAffineTransformScale(transform, 1, -1);
}
// Apply combined transformation
imgView.transform = transform;

How to rotate an layer at a specific anchor point without a skip?

I want to rotate a layer with an image at the top left corner, and not the center. According to the docs I set the anchorPoint property to [0, 1]. The view rotates in my example by 50°, but before it starts to animate, the view jumps to another point at the screen.
self.shakeTag.layer.anchorPoint = CGPointMake(0.0f, 1.0f);
[UIView beginAnimations:#"rotate" context:nil];
[self.shakeTag.layer setTransform:
CATransform3DRotate(CATransform3DIdentity,
radians(50.0), 0.0f, 0.0f, 1.0f)];
[UIView commitAnimations];
radians() is defined like this:
static inline double radians (double degrees) {return degrees * M_PI/180;}
When I use an image that is 4 times the size and has a lot of transparent pixels, I can rotate it at the default anchor point [0.5, 0.5], but I don’t want to waste the space for invisible pixels. Any ideas how I can prevent the layer from jumping before the rotation takes place?
Changing the anchor point affects the positioning of your view. You'll need to change the view's position if you change the anchor point and if you want to keep your view where it currently is. Use something like this (taken from here: Layer Position jumps at start of (Core) Animation) to set your anchor point and compensate position changes:
-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
{
CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y);
CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y);
newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
CGPoint position = view.layer.position;
position.x -= oldPoint.x;
position.x += newPoint.x;
position.y -= oldPoint.y;
position.y += newPoint.y;
view.layer.position = position;
view.layer.anchorPoint = anchorPoint;
}
Also see here for more details: Changing my CALayer's anchorPoint moves the view

Resources