UISlider's minView goes beyond it's bounds and thumb's center - ios

I wanted a custom slider and the requirement was the height of minView should be 8 pt & maxView should be 4 pt, it's the visible corners should be rounded.
I gave the slider's min & max image as clear image of alpha as 0.0
For maxView, I created a UIView and gave it width equal to slider's width, and made it's corners rouneded.
For minView, I created a UIView and it's leading is equal to leading of slider and trailing should be equal to the center of the thumb.
Here is the logic for what I did.
func changeValue() {
let difference = slider.maximumValue - slider.minimumValue
let thumbWidth = CGFloat(44/2)
let scale = (slider.frame.size.width-thumbWidth)/CGFloat(difference)
let width = scale * CGFloat(slider.value)
minSlideWidth.constant = width
self.view.layoutIfNeeded()
}
But the minView's width doesn't increase and decrease it's width with the thumb's center. If you see the image it had already crossed the UIslider's thumb.

How about this approach ?
Configure properties of UISlide control.
setMinimumTrackImage with 8pt image.
setMaximumTrackImage with 4pt image.
The track images could be re-created by stretchable image in order to keep the rounded corners. You can find it in the section Defining a Stretchable Image section from UIImage reference. It is very useful for track image of slide bar.

Related

How to make a button circular

I have a button which I am trying to make a perfect circle. I currently have used the code:
startButton.layer.masksToBounds = true
startButton.layer.cornerRadius = startButton.frame.width/2
But as you can see in the photo, the circle drawn in imperfect and cutting off some of the text.
Width and Height are equal.

Golden ratio corner rounded frame rectangle button

How can we make a rounded rectangle frame programatically, as solved with
buyButton.layer.cornerRadius = 2;
buyButton.layer.borderWidth = 1;
buyButton.layer.borderColor = [UIColor blueColor].CGColor;
in iOS 7 round framed button
but resulting in a golden ratio corner such as the one introduced in the iOS 7 icon shape;
?
If you want the same shape, just measure the image, eg with Photoshop and set the border radius proportional to the button's width. I did the measurement for you:
buyButton.layer.borderRadius = 0.315 * buyButton.frame.size.width;
I don't think it follows golden ratio thought. If it does, the ratio should be (1 - (sqrt(5) - 1)/2), which is 0.381..., which results in too rounded corners.
There's more to the golden ratio in this template. It appears in the relative sizes of the circles in relation to each other and the width of the template. See http://www.phimatrix.com/product-design-golden-ratio/ for details.

How to programmatically change a UIView's "origin" (as labeled in Xcode)?

You will notice the red "+" / "arrows" glyph in the attached screenshot. It is easy enough to change this "origin" point in Xcode. Is there also a way to do this programmatically or is this entirely an Xcode abstraction?
For instance, I want to programmatically create a UILabel and position it by calculating the lower right hand coordinate. In Xcode, I would simply make sure that the red "+" is on the bottom right grid point and define the X, Y, Width and Height parameters with that "origin" in mind.
If you're not using autolayout, you can position a label (or any view) in code by setting its center. So if you know where you want the label's lower right corner to be, you can just subtract half the width and height of the label to compute where its center should be:
CGPoint lowerRight = somePoint;
CGRect frame = label.frame;
label.center = CGPointMake(lowerRight.x - frame.size.width / 2,
lowerRight.y - frame.size.height / 2);
I would recommend just doing that.
But if you want, you can instead go to a lower level. Every view has a Core Animation layer, which is what actually manages the view's on-screen appearance. The layer has an anchorPoint property, which by default is (0.5, 0.5), representing the center of the layer. You can set the anchorPoint to (1, 1) for the lower-right corner:
label.layer.anchorPoint = CGPointMake(1, 1);
Now the label's center actually controls the location of its lower right corner, so you can set it directly:
label.center = somePoint; // actually sets the lower right corner
You'll need to add the QuartzCore framework to your target and import <QuartzCore/QuartzCore.h> to modify the anchorPoint property.
myObject.origin = CGPointMake (0.0,0.0);

UIView UIScrollview - frames, bounds, center confusions

I am putting a UIImageView inside a UIScrollView, and trying to control the image so that it is centred on the scrollview after a zoom. and I am not sure the best way to do this.
The apple docs tell us NOT to use the frame property: "Warning If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored." So I am attempting using the following in a UIViewController subclass whose xib contains a scrollView and contained imageView:
scrollView.bounds =
CGRectMake
(scrollView.contentSize.width/2 - scrollView.center.x,
scrollView.contentSize.height/2 - scrollView.center.y,
scrollView.bounds.size.width,
scrollView.bounds.size.height);
containedView.center =
CGPointMake
(containedView.bounds.size.width*scrollView.zoomScale/2,
containedView.bounds.size.height*scrollView.zoomScale/2);
This works accurately where the width and height of the containedView is larger than that of the scrollView and sets the views so that subsequent scrolling will take you exactly to the edges of the containedView. However when either dimension of the image is smaller than the scrollView width and height the image is magnetically attracted to the top left corner of the screen. In the iPad Simulator (only) when the images is shrunk to the size of minimumZoom it does lock on to the centre of the screen. The magnetic attraction is very smooth as if something in the UI is overriding my code after the image has been centred. It looks a bit like a CALayer contentsGravity ( kCAGravityTopLeft ) thing, maybe?
Apple contradict their own advice in their code sample, photoScroller (in a subclass of UIScrollView):
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
imageView.frame = frameToCenter;
This method does a better job of centring when the image is smaller, but when I try this on my project it introduces some kind of inconsistencies. For example, with scrollView.bounces = NO, a horizontal image whose height is smaller than the height of the scrollView but whose width is larger (so it can be scrolled from left to right) will scroll further to the left than it should (when scrolling to the right it stops correctly at the edge of the image, although if scrollView.bounces = YES it then bounces in from the edge so the image is always cropped on the left) When the image is larger in both dimensions than its containing scrollview this issue accentuates and the whole result feels broken, which is unsurprising given Apple's documented advice.
I have scoured the forums and can't find much comment on this. Am I missing something really obvious?
You don't appear to be using the transform property, so you can ignore that warning about not using the frame property when using the transform property. Go ahead and use the frame property, just like Apple (and the rest of us) do.

Scale a UIView with gradient

I'm developing an animated column bar chart and i having some trouble.
The bars show up with an scale animation. At the beginning all columns have 1 point size and will scale till a predefined height.
The point is: I want to draw a gradient into the columns (They are subclasses of UIView) and want the scale animation to recalculate the gradient.
Until now, i have drawn the gradient, but when the column scale, the gradient does not scale with it.
As the view have 1 point height, the gradient is show as 1 color only.
Can someone give me a north to solve this issue?
Solved the problem!
i was using scale instead of changing the view's bounds..
Was
view.transform = CGAffineTransformMakeScale(1.0, scaleY.floatValue);
Must be
view.bounds = CGRectMake(view.bounds.origin.x, view.bounds.origin.y * scaleY.floatValue, view.bounds.size.width, view.bounds.size.height * scaleY.floatValue);
Where scaleY was calculated before and is a NSNumber that contains the Y scale factor.
It's not necessary to set
view.contentMode = UIViewContentModeRedraw;
Thank you DarkDusk for the help.
Try setting the contentMode:
myView.contentMode = UIViewContentModeRedraw;
This will force a redraw on every size change.

Resources