Rotate Png Inside Uibutton

rotate png inside uibutton

Here's an example of how can you do it using Transforms:- The code works with all the elements that inherits from UIView

UIView.animate(withDuration: 0.5) {
self.yourButton.imageView?.transform = CGAffineTransform(rotationAngle: CGFloat.pi)

}

To set it back to original position:-

UIView.animate(withDuration: 0.5) {
self.yourButton.imageView?.transform = .identity)

}

Is any way to rotate UIButton Image without Button Rotation?

what about:

self.toggle.imageView?.transform = CGAffineTransform(rotationAngle: .pi/4)

iPhone: rotate the UIImageView inside a UIButton (Weird effect)

Okay, i finally got it.

It was needed to set the clipsToBounds property of the imageView to NO.

Sebstian

How can you rotate text for UIButton and UILabel in Objective-C?

[*yourlabelname* setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];

rotated image
Sample Image

pervious image
Sample Image

How do I animate a UIButton between two PNG images?

You can use animationImages property of your button's imageView:

myButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
nil];
myButton.imageView.animationDuration = 0.5; //whatever you want (in seconds)
[myButton.imageView startAnimating];

Your button will switch between your two images.

EDIT: As @tidbeck pointed the button needs to have an image assigned to create the imageview property.

Rotate UIImageView around its center point?

I found out that the code I need to accomplish what I needed was simpler than the one given by @Albin Joseph, but it did point me to the right direction.
My animation needs to pick up where it left off and rotate to a new position. Some times it will be animated and sometimes it won't. So hence the code:

CGFloat duration = animated ? 0.5 : 0.01;

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [[self.turnIndicatorImage.layer presentationLayer] valueForKeyPath:@"transform.rotation.z"];
animation.toValue = angle;
animation.duration = duration;
animation.fillMode = kCAFillModeForwards;
animation.repeatCount = 0;
animation.removedOnCompletion = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.turnIndicatorImage.layer addAnimation:animation forKey:@"transform.rotation.z"];

UIButton in custom UITableViewCell stop working after rotation

Have you checked how the superview is being resized?

Check if the superview has 'clip to bounds' checked. If it is not check it. That will make the view clip its contents so you see if it is resizing ok.

I'd say the superview is not sizing correctly and because of that the touch events are not well delivered also.

EDIT - So this was the tip that could let the OP reach the solution:

What I normally do in cases like of unexpected behavior in resizing and such is to change every view in the hierarchy to a different, well recognizable, color. Right now you have view A and view B with the same background color (or clear) and you don't see if view B is resizing well. Good luck.

Rotate UIButton titleLabel without clipping

For anyone coming to this in the future, I ended up creating a new UIView (the blue rectangle) which contained the UIButton (the white '+'). I then disabled used interaction on the UIButton, and added a gesture recogniser to the UIView. When I wanted to rotate the '+', I rotated the whole UIButton (but not the containing UIView).



Related Topics



Leave a reply



Submit