iOS gradient fill in text animation - ios

In iOS Objective C, I need the text fill animation with gradient, the animation will fill the gradient from bottom to top (Vertically). I am not good in core graphics or animation things in iOS.
Has anyone did this kind of task before.
Any help would be really appreciated.

Here is an idea. Use two labels and animate the second one in. This has its limits, to go further you need to either use the labels' layers or to convert it to UIImage's and then animate that in. This animates from the center out which is not entirely what you are looking for, but just an idea for now.
The storyboard contains just one button which you need to wire up to kick off the animation.
EDIT
Previously it animated from the center outwards but I've changed it to animate from the bottom to the top.
- ( void ) viewDidLoad
{
[super viewDidLoad];
// Start label
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake ( 10, 50, 100, 20 )];
// Configure
label.text = #"Label";
label.textColor = UIColor.redColor;
label.sizeToFit;
[self.view addSubview:label];
// The label we animate in
UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake ( 10, 50, 100, 20 )];
// Configure
label1.text = #"Label";
label1.textColor = UIColor.blueColor;
label1.clipsToBounds = YES;
label1.contentMode = UIViewContentModeBottom;
label1.sizeToFit;
// We keep a weak reference to it
[self.view addSubview:label1];
self.label1 = label1;
}
- ( IBAction ) buttonAction:( id ) sender
{
CGRect f = self.label1.frame;
CGRect b = self.label1.bounds;
// Need to animate both the bounds and the frame's position
self.label1.frame = CGRectMake( f.origin.x, f.origin.y + f.size.height / 2, f.size.width, f.size.height );
self.label1.layer.bounds = CGRectMake( 0, 0, b.size.width, 0 );
[UIView animateWithDuration:2
animations:^{
self.label1.frame = f;
self.label1.layer.bounds = b;
}];
}
Note that you need to add a weak property to the view controller, e.g.
#property (nonatomic,weak) UILabel * label1;
as you need this later to be able to animate.

Related

IOS/Objective-C: Subview of Subview not displaying

I am trying to create a chart where a bar in the form of a UIView displays on top of a background UIView. I'd like both to display on top of the UIView for the whole screen. I have done this before successfully, but while I can get the first view to display, I somehow can't get my code to display the bar. Could it have something to do with setting the color? Or can anyone suggest why the second subview is not displaying.
My code:
//Make background box:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat graphWidth = screenWidth-40;
CGFloat graphHeight = 160;
CGRect graphBounds =CGRectMake(20, 200, graphWidth, graphHeight);
float tableStartY = graphBounds.origin.y;
UIView *graphBox = [[UIView alloc] initWithFrame:graphBounds];
graphBox.backgroundColor = [UIColor colorWithRed:200.0/255.0 green:200.0/255.0 blue:200.0/255.0 alpha:0.2]; graphBox.layer.borderWidth = 1;
graphBox.layer.borderColor = [UIColor blueColor].CGColor;
//Make Bar
CGFloat barWidth = 20;
CGFloat barHeight = 100;
CGRect aBar = CGRectMake(20, tableStartY+1, barWidth, barHeight);
UIView *barView = [[UIView alloc] initWithFrame:aBar];
barView.backgroundColor = [UIColor blueColor];
barView.layer.borderWidth = 1;
barView.layer.borderColor = [UIColor redColor].CGColor;
// [graphBox addSubview:barView];
[self.view addSubview: graphBox];
If I run the above code, it displays the graphBox. If I add the bar directly to the view as a subView instead of the graphBox, the bar displays. However, if I uncomment out the line shown and add the barView first to the graphBox and then add the graphBox to the view, the barView does not display.
Thanks in advance for any suggestions.
If I understand correctly what you need to do, you should replace
CGRect aBar = CGRectMake(20, tableStartY+1, barWidth, barHeight);
with
CGRect aBar = CGRectMake(20, 1, barWidth, barHeight);
[edit: and obviously uncomment the addSubview line]
Perhaps this is an accident in your posted code, but you have specifically commented out where the barView would be added to the screen.
// [graphBox addSubview:barView];
In addition, as another answer lists, your offset is incorrect if you are adding barView to graphBox. If you add it to self.view instead, your offset is correct.
So, you've got two choices, depending on the containment you desire in your view hierarchy:
CGRect aBar = CGRectMake(20, 1, barWidth, barHeight);
// ...
[graphBox addSubview:barView];
or
CGRect aBar = CGRectMake(20, tableStartY+1, barWidth, barHeight);
// ...
[self.view addSubview: graphBox];
[self.view addSubview:barView];
Note that in the second option, the order is important to get the barView to display over top of the graphBox as they will be siblings.

"Fill button" animation in iOS

Do you have any suggestion to implement the following button animation in an iOS app?
Ok lets split this animation into a few segments to figure it out.
1) We need a white and green circle.
2) We need a tick image.
3) We need to animate the white circle and the tick.
Step One
Add a green UIView to your interface file. This will be the background view. We do not need to animate this view. Then link the green view to a IBOutlet like so:
IBOutlet UIView *greenView;
Step Two
Add a white UIView inside of the green UIView and make it the exact same size as the green UIView. Then link the white UIView to an IBOutlet in you header file like so:
IBOutlet UIView *whiteView;
Step Three
Make or download a 'tick' image and add it to your Xcode project. Then add the tick image ABOVE the green/white views that we just created. Please place the tick above the views and NOT in the views. Then link the tick image to an IBOutlet - this will help us detect when the view has been pressed. Do it like so:
IBOutlet UIImageView *tickImage;
Step Four
We need to change the green and white UIView to circles. We can do this with code. Import the #import <QuartzCore/QuartzCore.h> framework into you header file.
Then in your viewDidLoad method, add the following code which will change the green/white views into circles:
CGPoint greenCenter = greenView.center;
CGPoint whiteCenter = whiteView.center;
CGRect newFrameGreen = CGRectMake(greenView.frame.origin.x, greenView.frame.origin.y, 20, 20);
CGRect newFrameWhite = CGRectMake(whiteView.frame.origin.x, greenView.frame.origin.y, 20, 20);
greenView.frame = newFrameGreen;
greenView.layer.cornerRadius = newSize / 2.0;
greenView.center = greenCenter;
whiteView.frame = newFrameWhite;
whiteView.layer.cornerRadius = newSize / 2.0;
whiteView.center = whiteCenter;
Step Five
In order to run the animation, we need to connect the image view to a method which will run the animation. You can do it by using a UITapGestureRecognizer. In your viewDidLoad method, add the following:
UITapGestureRecognizer *newTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tickAnimation)];
[tickImage setUserInteractionEnabled:YES];
[tickImage addGestureRecognizer:newTap];
Then add the animation method to you implementation (.m) file as well:
-(void)tickAnimation {
}
Step Six
Create a BOOL value in your header file - we will use this to detect if the button is in the "ON" or "OFF" states.
BOOL animationMode;
Then in your viewDidLoad method, set the animationMode bool value to "OFF" - this is because the button will be "OFF" or in the white state by default.
animationMode = NO;
Now we can go on to the animation. From what I can see, this is split up into two animations. The first one is making the white view smaller and the second one is doing a subtle popup animation on the tick image. Update the tickAnimation method in order to perform the animation:
-(void)tickAnimation {
if (animationMode == NO) {
// Button is OFF or set to white, lets set it to ON.
[UIView animateWithDuration:1.0f animations:^{
whiteView.transform = CGAffineTransformMakeScale(0,0);
} completion:nil];
[UIView animateWithDuration:1.0f animations:^{
tickImage.transform = CGAffineTransformMakeScale(2,2);
} completion:nil];
[UIView animateWithDuration:1.0f animations:^{
tickImage.transform = CGAffineTransformMakeScale(0,0);
} completion:nil];
animationMode = YES;
}
else if (animationMode == YES) {
// Button is ON or set to green, lets set it to OFF.
[UIView animateWithDuration:1.0f animations:^{
whiteView.transform = CGAffineTransformMakeScale(1,1);
} completion:nil];
animationMode = NO;
}
}
This is my attempt at making this kind of animation. I hope this helps :)
You could animate the layer.borderWidth property for the colour fill.. It draws inside of the layer, something like this.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
[self.view addSubview:button];
button.layer.borderWidth = 10;
button.layer.borderColor = [UIColor greenColor].CGColor;
button.layer.cornerRadius = 50;
CABasicAnimation *border = [CABasicAnimation animationWithKeyPath:#"borderWidth"];
[border setFromValue:[NSNumber numberWithInt:10]];
[border setToValue:[NSNumber numberWithInt:50]];
border.duration = 1.0;
button.layer.borderWidth = 50;
[button.layer addAnimation:border forKey:#"border"];
Here is a really slow gif screen cap of this working with autoReverse set to true.
If you like Swift it looks like this
var button = UIButton(frame: CGRect(x: 30, y: 30, width: 100, height: 100))
self.view.addSubview(button)
button.layer.borderWidth = 10
button.layer.borderColor = UIColor.greenColor().CGColor
button.layer.cornerRadius = 50
let border:CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
border.fromValue = 10
border.toValue = 50
border.duration = 1.0
button.layer.borderWidth = 50
button.layer.addAnimation(border, forKey: "border")
First, you have an imageView with image like this.
Second, you overlay it with a white UIView, and at the same time, init a UIImageview for the v symbol to turn it into white color.
Next step, run animation to scale the white view from 1 to 0 in 0.2 sec.
When it done, you run other animation to make the white v symbol scale from 1 to 1.2 in 0.2 sec.
That is my idea.

iOS - center content of UIView

I am trying to create UIView with image and number. I want to center them. For now I have this code:
CGRect likesRect = CGRectMake(0,152, screenWidth / 2, 30);
MARoundedRect *likesRoundedRect = [[MARoundedRect alloc] initWithFrame:likesRect];
UIView *likesView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
likesView.center = CGPointMake(screenWidth / 4, 20);
UIImageView *likeImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
likeImage.image = [UIImage imageNamed:#"favorite.png"];
UILabel *likesLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, screenWidth / 2, 20)];
[likesLabel setTextColor:[UIColor whiteColor]];
[likesLabel setBackgroundColor: [UIColor clearColor]];
[likesLabel setText:[actualPlace.Favorites stringValue]];
[likesView addSubview:likeImage];
[likesView addSubview:likesLabel];
[likesRoundedRect addSubview:likesView];
[viewController.view addSubview:likesRoundedRect];
For now it's working good but I am setting width of likesView to exact value and I guess this goes bad when I don't have number with one or two letters but more. How can I fix this? How to center content of UIView if there are two or more components with side by side? Or how can I set UIView to dynamically change width by content? Thanks
I am not writing full code but you try with following code,
CGRect likesRect = CGRectMake(0,152, screenWidth / 2, 30);
MARoundedRect *likesRoundedRect = [[MARoundedRect alloc] initWithFrame:likesRect];
CGSize likeViewSize = CGSizeMake(50,30);
UIView *likesView = [[UIView alloc] init];
likesView.frame = CGRectMake(likesRoundedRect.frame.size.width/2 - likeViewSize.width/2,likesRoundedRect.frame.size.height/2 - likeViewSize.height/2,likeViewSize.width,likeViewSize.height);
I have centered the likesView on its parentview. you have to do same thing for label and imageview if you want to center them on their parentview (likesView)
UIView and its subclasses has a property called center :
CGPoint center1 = CGPointmake(self.view.bounds.size.width/2,
self.view.bounds.size,height/2) ;
subview.center = center1;
The following discussion is from the Apple documentation on UIView:
#property(nonatomic) CGPoint center
Discussion
The center is specified within the coordinate system of its superview and is measured in points. Setting this property changes the values of the frame properties accordingly.
Changing the frame rectangle automatically redisplays the receiver without invoking the drawRect: method. If you want the drawRect: method invoked when the frame rectangle changes, set the contentMode property to UIViewContentModeRedraw.
Changes to this property can be animated. Use the beginAnimations:context: class method to begin and the commitAnimations class method to end an animation block.
Availability
Available in iOS 2.0 and later.
See Also
#property frame
#property bounds
#property transform
Related Sample Code
AVLoupe
Handling Touches Using Responder Methods and Gesture Recognizers
iAdInterstitialSuite
iAdSuite
RosyWriter
Declared In
UIView.h

iOS: adding images to a UIButton, then putting button in a scrollview, but only seeing one image?

I'm working on an iPad app and part of the UI is a scrollview that has buttons for its contents. I'm working on adding images to these buttons, but when I do so, I only ever get the image in one spot, when I should be seeing it on all the buttons. This is what I'm doing:
float scrollCurrTop = 0;
CGRect currProcedureButtonFrame = CGRectMake(0,
scrollCurrTop,
self.fProceduresView.frame.size.width,
self.fLabelAndButtonHeight);
PatientIDButton* currProcedureButton = [PatientIDButton buttonWithType:UIButtonTypeCustom];
[currProcedureButton setFrame:currProcedureButtonFrame];
[currProcedureButton.layer setBorderColor: [self.fPanelViewBorderColor CGColor]];
[currProcedureButton.layer setBorderWidth: self.fBorderWidth];
currProcedureButton.titleLabel.font = self.fLabelFont;
NSString* displayName = [grabbing name];
if (displayName == nil)
{
displayName = currPlanName;
}
[currProcedureButton setTitle:displayName
forState:UIControlStateNormal];
[currProcedureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
currProcedureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
currProcedureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// is the plan approved?
if ([self isPlanApproved:currPlanName])
{
// add the checkmark to this plan button
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
[currPlanApprovalImage setBackgroundColor:[UIColor colorWithRed:(63.0f/255.0f)
green:(179.0f/255.0f)
blue:(79.0f/255.0f)
alpha:1.0f]];
[currPlanApprovalImage setImage:self.fCheckMarkIcon];
[currProcedureButton addSubview:currPlanApprovalImage];
}
[self.fProceduresView addSubview:currProcedureButton];
scrollCurrTop += self.fLabelAndButtonHeight;
Where 'fProceduresView' is the scrollview that houses the buttons. What am I doing wrong?
It seems that you're misunderstanding the logic behing setting the frame of the imageview's those you're trying to add
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
You don't need to set originY to currPlanButtonFrame.origin.y;
All subviews have relative coordinates to their's superviews.
So in your case originY should be 0
For example:
// Image is visible
Button frame [0, 0, 100, 100]
image in button [0, 0, 100, 100]
// Button is visible, but image is not because it will be clipped by bounds of the button.
Button frame [100, 100, 100, 100]
image in button [100, 100, 100, 100]
Also you will be able to "see" your images, if you set
currProcedureButton.clipsToBounds = NO

Show progress line over existing elements

Not sure if the title is very clear but I don't know the exact name for the thing I'm looking for. I have a gird that consists of UIButtons (basically a sequencer). I want a line similar to the vertical line in this image (between the 8th and 9th button of each row:
So basically when my grid is being played I want to indicate the progress. Any idea how I could do this? What kind of elements to use etc.
You can add a UIView that is very narrow, 1 or 1.5 pixels. Set the background color to white or a gradient. Add this view to the same subview as the buttons.
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(100.0, 0.0, 1.5, 320.0)];
lineView.backgroundColor = [UIColor whiteColor];
[parentView addSubview:lineView];
To show progress you'll need to know how much is left until completion. Take that value and divide by 320 to get the lineView height.
CGRect frame = CGRectMake(100.0, 0.0, 1.5, 0);
lineView.frame = frame; // 0% complete
frame = CGRectMake(100.0, 0.0, 1.5, 160.0);
lineView.frame = frame; // 50% complete
frame = CGRectMake(100.0, 0.0, 1.5, 320.0);
lineView.frame = frame; // 100% complete
That is the idea. Hope this help.
Update to vertical bar moving across the screen
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1.5, 320.0)];
lineView.backgroundColor = [UIColor whiteColor];
[parentView addSubview:lineView];
As progress is made
- (void)progressWithValue(CGFloat)progress {
CGRect frame = lineView.frame;
frame.origin.x = progress; // Change x position
lineView.frame = frame;
}
You can use UISlider to show the progress of your events or activity(not so clear of your code) and customize it according to your needs.You can look at the UICatalog sample code by Apple to get to know-how of customizing a UISlider along with other elements or UISlider Tutorial

Resources