is there a possibility to animate an uibutton in this way that a thing or whatever comes out of the background e.g. a wheat ear comes out of a field.
is this possible?
thanks :)
Unless you want a more complicated animation, you can just create a custom type UIButton (either with IB by changing the type to Custom, or programmatically with UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]), then set the button's image with [aButton setImage:[UIImage imageNamed:#"wheat_ear" forState:UIControlStateNormal].
To move it, just animate its position normally...
[UIView animateWithDuration:0.5 animations:^{aButton.center = newCenter;}];
Or, to provide the illusion of it coming out of the field, animate the bounds of the image so that it starts with the image's width and zero height, and ends with the image's width and height:
CGRect originalFrame = aButton.frame;
aButton.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y, originalFrame.size.width, 0);
[UIView animateWithDuration:0.5 animations:^{aButton.frame = originalFrame;}];
Related
I'm trying to give my UIButton a new background image when the user pressed the button and then animate the width. Problem is that the button doesn't scale the image (I'm only using retina images).
Before click:
After click:
And the code that makes things happen:
UIImage *buttonProfileDefault = [[UIImage imageNamed:#"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];
[self.profileButton removeConstraint:self.profileButtonConstraint];
// Animate
CGRect originalFrame = self.profileButton.frame;
originalFrame.size.width = 40;
[UIView animateWithDuration:0.2f
animations:^{
self.profileButton.frame = originalFrame;
}
completion:^(BOOL finished) {
}];
try this code...
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[self.profileButton setBackgroundImage:[UIImage imageNamed:#"Profile_Button_Default"] forState:UIControlStateNormal];
[self.profileButton setBackgroundImage:[UIImage imageNamed:#"Profile_Button_Default"] forState:UIControlStateSelected];
[self.profileButton setFrame:CGRectMake(self.profileButton.frame.origin.x, self.profileButton.frame.origin.y, self.profileButton.frame.size.width + 40, self.profileButton.frame.size.height)];
[UIView commitAnimations];
i hope this help you...
The way you are creating the Edge Insets, would keep the leftmost 3 and the rightmost 3 poins (or pixels) constant, and it would stretch the head on the button. If you want the head's size to be constant, you have to include it in the left side, leave 1 pixel stretchable and then comes the right side. Assuming your picture's width is 50, you would write:
UIImage * buttonProfileDefault = [[UIImage imageNamed:#"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 46, 3, 3)];
However, this would make your picture positioned in the left side of the button. The solution I recommend would be to use 2 images:
a resizable image containing only the border of the button, set as the button's background image
an image containing only the head, set as the buttons image with constant size
The code would look something like:
UIImage *backgroundImage = [[UIImage imageNamed:#"border.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)]; // in this case you can use 3, 3, 3, 3
[self.profileButton setBackgroundImage:backgroundImage forState:UIControlStateNormal];
UIImage *titleImage = [UIImage imageNamed:#"head.png"];
[self.profileButton setImage:titleImage forState:UIControlStateNormal];
Hope this helps!
I have modified your code on following points:
provided complete name of image Profile_Button_Default.png (or .jpg etc),
added states for image as in case of normal it these effects will not be appeared in starting of button touched
Now Check:
UIImage *buttonProfileDefault = [[UIImage imageNamed:#"Profile_Button_Default.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateNormal];
[self.profileButton setBackgroundImage:buttonProfileDefault
forState:UIControlStateSelected];
[self.profileButton setBackgroundImage:buttonProfileDefault forState:UIControlStateHighlighted];
[self.profileButton removeConstraint:self.profileButtonConstraint];
// Animate
CGRect originalFrame = self.profileButton.frame;
originalFrame.size.width = 40;
[UIView animateWithDuration:0.2f
animations:^{
self.profileButton.frame = originalFrame;
}
completion:^(BOOL finished) {
}];
I think the problem is that your changing the background and then resizing.
The key to getting it working with auto layout is to animate the widthConstraint constant value and also set the background image in an animation.
For example, I have a button on my view name button, I have an IBOutlet to my width constraint named buttonWidth - Below is the code for my buttonPress
:
UIImage *imageButton = [[UIImage imageNamed:#"button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
[UIView animateWithDuration:0.01f
animations:^{
[self.button setBackgroundImage:imageButton forState:UIControlStateNormal];
}
completion:^(BOOL finished) {
[UIView animateWithDuration:1.0f
animations:^{
self.buttonWidth.constant = 300;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
}];
}];
As you can see with my code above, the key to making it work for me was setting the button background inside an animation also, for some reason if i changed the background image outside the animation it would not set it correctly until the animation completed.
Also setting the frame when using autoLayout didn't work for me, so I animated the constraint constant
Try this, instead of assigning the image as
UIImage *buttonProfileDefault = [[UIImage imageNamed:#"Profile_Button_Default"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 3, 3, 3)];
do this
UIImage *buttonProfileDefault = [UIImage imageNamed:#"Profile_Button_Default"];
no need for setting constraint of resizableImageWithCapInsets.
Could you not make a copy of the image and resize it to the correct button size?
Check out IWasRobbed "s answer in this post.
He has a nice function there for you and claims to use this method for his buttons.
I had this same problem, but it happened when my app loaded up, I messed around with the button's attributes in the inspector and if you scroll down and Un-check autoresize subviews it might work, it did for me!
I've had a similar situation resizing subviews when using a nib. Not sure if you are or not, but what fixed my problems was unchecking 'Use Autolayout' on my various views.
I am having two UIButtons(button1 and button2) of two different frames, the thing is I want to drag the button2 on to the button1 so that the button2 is repositioned to button1 position, and button2 should occupy button1 position.
Guy's I need ur help and ideas how can I do with it.
Thanks to all,
Monish
CGRect rect = btn.frame;
btn.frame = btn2.frame;
btn2.frame = rect;
If you want to animate this, start with reading this
Is it what you are asking for?
You need to change the button's x-axis in frame and use animations to show it as its moving (if you want) accordingly.
[buttonOutletName setFrame:CGRectMake(old_xCoordinate, yCoordinate, width, height)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 1.0];
[buttonOutletName setFrame:CGRectMake(new_xCoordinate, yCoordinate, width, height)];
[UIView commitAnimations];
If helpful please mark :)
I have seen quite a few tutorials on how to fade images in and out on the iPhone simply by changing their alpha values (for example here).
Thats ok, however I am trying to achieve something slightly different; instead of the whole image fading in/out, I would the image to be animated so that over time it is displayed from the centre of the image to the edge (and vice versa).
So at the start of the animation I would like the image to not be displayed on the screen and then over the duration of the animation starting from the centre bit by bit the image is displayed until we reach the end and the whole image is displayed on screen.
I cant find anything along those lines, does anyone know if that would be possible on the iPhone? Any ideas/links that might be useful?
It should be easy enough to achieve — create a UIImageView holding the thing, set contentMode to UIViewContentModeCenter, then use a CoreAnimation block to animate a change in the frame from being of zero width/height and centred to being the full area you want to cover.
E.g.
/* coming into here, imageView is a suitable UIImageView,
frameToFill is a CGRect describing the area the image
view should cover when fully unfurled */
// set up image view: content mode is centre so that the current
// frame doesn't affect image sizing, and we'll be keeping the same
// centre throughout so it won't move. The initial frame is on the
// centre and of zero size. The view contents are clipped to the view's
// bounds so that the changing frame does actually cut off the image
imageView.contentMode = UIViewContentModeCenter;
imageView.frame = CGRectMake(frameToFill.origin.x + frameToFill.size.width*0.5f,
frameToFill.origin.y + frameToFill.size.height*0.5f,
0.0f, 0.0f);
imageView.clipsToBounds = YES;
// set up an animation block, lasting 3 seconds, in which the
// only thing animated is the view's frame, expanding back out
// to the original
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.0f];
imageView.frame = frameToFill;
[UIView commitAnimations];
The edge of your view will be hard and rectangular. If you want anything more subtle, you're probably not going to be able to achieve it with CoreAnimation; dropping down to the CoreGraphics level is probably the order of the day.
A simple (and possibly not the best) thing to try (no guarantees it works) is to create an imageView and round the corners until it's circular, something like this:
[imageView.layer setCornerRadius:60.0f];
[imageView.layer setMasksToBounds:YES];
[imageView.layer setBorderWidth:0.0f];
Set the initial imageView small (1px by 1px). Then use some simple animation code:
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelay:2.0];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
// set the imageView to the size you want here
} [UIView commitAnimations];
Try this code.
Create an imageView.
UIImageView *imgv = [[UIImageView alloc]initWithFrame:
CGRectMake(0, 0, 0, 0)];
imgv.image = [UIImage imageNamed:#"black.png"];
//position the imageview to the center of the screen.
imgv.center = self.view.center;
[self.view addSubview:imgv];
[imgv release];
//now give the imageview a new frame of full size with an animation
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
//i am assuming that your full frame is 320x480
imgv.frame = CGRectMake(0, 0, 320, 480);
[UIView commitAnimations];
Now the imageview will appear to be scaling from the center of the screen to fill it.
I've got a strange problem using UIButtons, type custom. I'm placing 4 of those buttons on a UIScrollview, rotating each button by a random angle using CGAffineTransform. Now it seems that the buttons themselves change size depending on the angle of rotation.
UIGraphicsBeginImageContext(tempCtxSize);
[cookbookImage drawInRect:CGRectMake(imgOffsetX, imgOffsetY+frmOffsetY, cookbookImage.size.width, cookbookImage.size.height)];
[cookbookFrame drawInRect:CGRectMake(0.0f, frmOffsetY, cookbookFrame.size.width, cookbookFrame.size.height)];
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
…
UIButton *cookbookViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cookbookViewButton setFrame:CGRectMake(0.0f, 0.0f, combinedImage.size.width, combinedImage.size.height)];
[cookbookViewButton setBackgroundColor:[UIColor clearColor]];
[cookbookViewButton setBackgroundImage:combinedImage forState:UIControlStateNormal];
CGAffineTransform rotation = [cookbookViewButton transform];
rotation = CGAffineTransformRotate(rotation, angle); // some random angle
[cookbookViewButton setTransform:rotation];
This is a system bug:
" Important If a view’s transform property does not contain the identity transform, the frame of that view is undefined and so are the results of its autoresizing behaviors."
from: Handling Layout Changes Automatically Using Autoresizing Rules
Solution: set the parent view's autoResizeSubviews to NO.
parentView.autoresizesSubviews = NO;
Don't set the frame of cookbookViewButton - set the bounds.
So there seems to be an issue with animating size transformations for UIButtons. Specifically, if you have a button of type UIButtonTypeCustom, any frame size transformations happen instantly. Movement and other animations happen as normal.
Does anyone have a good fix for this? I'm guessing that it's because the custom buttons contain images, and something is going wrong when UIView calculates its transformations.
This guy seems to have found the same problem, but no workaround.
Here's an example. The image origin will move smoothly from 0.0,0.0 to 100.0,100.0 over two seconds, but the size instantly jumps to 200x200.
UIButton *tButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tButton setBackgroundImage:tImage forState:UIControlStateNormal];
tButton.frame = CGRectMake(0.0, 0.0, 10.0, 10.0);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:2.0];
tButton.frame = CGRectMake(100.0, 100.0, 200.0, 200.0);
[UIView commitAnimations];
Credit goes to davbryn for this answer but instead of redefining the frame you use:
button.transform = CGAffineTransformMakeScale(1.5,1.5);
Its not as easy as just using a new frame so you will have to play around with the it but it should get you pointed in the right direction.