UISlider change width of lines drawn on a view? - ios

I am currently working on a drawing application in swift. So far I have implemented buttons that will change the colour of the drawings. I now have a UISlider and want to change the width of the drawings being made. Does anyone know
how this could be implemented, as in what code can I use for the IBAction function in the view controller?

You can use Value changed event.
Inside value changed event you can get the slider.value assign this value to the width factor.
-(IBAction)SliderValueChanged:(id)sender
{
UISlider * slider = (UISlider*)sender;
self.drawingWidth = slider.value;
//redraw with the new value
}

I think you have overrided touchesBegan drawLineFrom touchesMoved methods of the drawing view.
I came accross a well explained example for you. It explains how to control coloring, opacity and brush sizes as well.
Have a lok at this example

Related

Adding button gradient before appearing in iOS

I want to use gradient for a button in iOS, but since the button's frame (button.layer.bounds) changes because of Auto Layout, I found that I have to put the insertSublayer in the viewDidAppear method -- which means the button first appears without the gradient and then gets repainted with it. Is there anyway to get the final button size in viewWillAppear so that I can apply the correct size gradient before it appears?
Here's how I do it now:
- (void)viewDidAppear:(BOOL)animated{
CAGradientLayer *gradient1=[CAGradientLayer layer];
gradient1.colors=[NSArray arrayWithObjects:(id)light.CGColor,(id)dark.CGColor,nil];
gradient1.cornerRadius=10;
gradient1.frame=_go_button.layer.bounds;
gradient1.borderColor=dark.CGColor;
gradient1.borderWidth=1;
[_go_button.layer insertSublayer:gradient1 atIndex:0];
Sometimes by asking a question leads you to finding your own answer.... Here's a link to a website I found that answered it: has the solution
The simple answer is to put the gradient code into viewDidLayoutSubviews instead of viewDidAppear.

UIView content mode acts differently when override drawRect:

Im learning iOS. I've been reading Apple's View programming guide.
I did the followings to try out content mode:
I dragged a uiview into storyboard and made it my CustomView class.
Then i changed the content mode to be left and set the background color to be pink color.
Then there's a button that just simply changes the custom view's width and height to be 1.5 times bigger.
Now, i found an interesting thing:
I run this, and click the button, then:
Case 1: If i override the draw rect method
drawRect: overrided
Then the appearance of the view shifted down
Case 2: Without overriding the draw rect method
drawRect: not overrided
The size actually increased
After doing some search online and look into Apple's document, i couldnt any relavent things except one question mention that this might have something to do with drawLayer:inContext: behave differently if override the drawRect:
Does anyone know what is going on here?
(sorry about the formatting, this is a new account and i can't post more than 2 links.)
CODE:
For the CustomView, just either override drawRect or without it.
#import "CustomView.h"
#implementation CustomView
- (void)drawRect:(CGRect)rect{
//Do nothing, for case 2 i just commented out this method.
}
#end
For Changing the frame of customView:
- (IBAction)changeBound:(id)sender {
self.customView.frame = CGRectMake(self.customView.frame.origin.x, self.customView.frame.origin.y, self.customView.frame.size.width * 1.5, self.customView.frame.size.height * 1.5);
}
After doing some research, i think i figure out what happened:
So drawing background code is actually handled separately in -(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context. That's why with empty drawRect, it still draws the background. Also, depending on the existence of drawRector not(Probably by calling respondsToSelector?), UIView behave different in drawLayer :inContext.
See Dennis Fan's answer in this link

Swift: Change label text value as animation plays

I've followed this answer to create an animated circle, and it works just fine. In addition to the path, I draw a text in the center of the view with drawInRect. I would like that text to change during the animation, to reflect the percentage of the path that is visible at each moment. How can I achieve that?
Thanks.
The most promising post would likely be this one. It describes creating a new CALayer that keeps track of the progress, which can then fire a call back.
Otherwise you'll likely need to wangle something together by observing your view's presentationLayer.

Facebook Paper-like table cells animation

I'm trying to implement transition used in settings menu in Facebook's Paper app: http://blog.brianlovin.com/design-details-paper-by-facebook/#1. I'm using my custom UIViewControllerAnimatedTransitioning object, but I can't achieve the same effect, no matter what type of animation I'm using. So I have some questions:
1) Which animation is used? It looks like cells start to move really fast, and then halt to their final position. Looks like I need POPDecayAnimation, but the result isn't even close.
2) Is delay between animations achieved with setting animation's beginTime depending on cell's index? Or first cells have bigger velocity than last cells?
As Injectios suggested AMWaveTransition is kinda close to Paper's animation. I ended up using it with some adjustments.
I also asked this question on pop github page, you can view it here. Their answer might be useful if you want to implement this animation yourself:
Hey! I implemented that in Paper. The animations on each cell are POPSpringAnimations, and they're connected together by (every frame) updating the toValue of each animation to point to the current value of the one before. Hope that helps!
In cellForRowAtIndexPath, use UIViewAnimation block.
Set cell properties as per your requirement:
indentationLevel
indentationWidth
shouldIndentWhileEditing
Then in animation block, change frame position.
For example for first cell, indetationLevel = 1, and indentationWidth = cell.frame.size.width, then update it to indetationLevel = 0 and indentationWidth = 0 in that animation block. And for the further cells you can multiply this values by (cellIndexPath+1)
For more details visit : https://developer.apple.com/library/ios/Documentation/UIKit/Reference/UITableViewCell_Class/index.htm
P.S.: Update the logic as per your requirement

How to make UISlider thicker when it is first displayed?

I am trying to make a UISlider that has a fat track. I tried this, and it almost works. However, when the UISlider is first displayed, the track bar is normal. Only when it gets a touch event does it get thicker.
Here is the code I added to my subclass of UISlider, as recommended:
- (CGRect)trackRectForBounds:(CGRect)bounds
{
return bounds;
}
I don't understand how this works, so I can't figure out why it isn't working perfectly. Here are my questions:
How does this work?
How can I control how thick it makes the track?
How can I make it affect the track when the UISlider is first drawn?
Of course, the third question is the most important, though the others will help me in my understanding.
One other thing to consider: when it is first displayed, I hide the thumb:
[slider setThumbImage:[[UIImage alloc] init] forState:UIControlStateNormal];
When the user touches the slider, I display the thumb like this:
[slider setThumbImage:nil forState:UIControlStateNormal];
I mention this because I wonder if that is somehow interfering with the trackRectForBounds magic.
Thanks!
Question 1. From the docs:
If you want to customize the track rectangle, you can override this
method and return a different rectangle. The returned rectangle is
used to scale the track and thumb images during drawing.
The default implementation of this method returns a smaller rectangle than the one it is passed. Yours simply returns what it's passed, instead of shrinking it first.
Question 2. Return a larger or smaller CGRect, for example:
return CGRectMake(0, 0, 500, 200);
Question 3. I think the method only gets called if you don't set a custom image, which is why it has no effect until you set the image to nil. If you want to override the image, try overriding the slider images with actual images that look the way you want.

Resources