I wonder what the best way to create a horizontal line is?
I have a few labels / text and I would like to add a horizontal line between them. More or less like a when using twitter bootstrap.
Right now I am using a UIview and setting it as 1 in height with a black background color. But I guess this isnt the best way to do it?
That is the way to do it in iOS. UIView is the class which represent something to be shown on the screen. Every UI component is direct or indirect descendant of UIView. So, you should use UIView.
UIView is backed by CALayer to render the content to screen. You could also use CALayer, CAShapeLayer or other layer classes to create a border. But, I would not recommend to use CALayer just to show a border as it is simpler to use UIView with height of 1, on the top of that, you get some nice addition such as autolayout. If you use CALayer, you will have to set frame to layer at appropriate time when the view bounds change. It could also be tricky due to some intrinsic animation within CALayer.
Related
So basically i have two image objects, 1 portrays a line and the other a circle. I wanted to know if its possible using something like CABasicAnimation to loop the line image across the x axis but still keeping it within the circle image?
Yes, you should be able to do this with either CABasicAnimation or UIView animation.
CAAnimations only operate on CALayers, and they are rather tricky to use. I suggest using UIView animations on UIImageViews or other UIViews instead. You could even set up a custom subclass of UIView to have a CAShapeLayer as it's backing layer and then animate the custom UIView using UIVIew animations
I am exploring the idea of drawing some custom primitives (using CGContext) on a view that is scrollable and larger than the phone screen width.
The idea would be to use the "power" of a UIScrollView by programmatically scrolling the content of the view as the content is added and decouple in this way the scrolling handling (and general UI interaction with the view) from the content drawing.
Is this a feasible approach in iOS?
Yes it is. The easiest approach AFAIK would be to add a UIView onto the UIScrollView. You would then draw on that UIView instance - after drawing another part of your graph/image you would need to inform the containing scroll view, via a delegate for example, that it needs to update its contentSize. This would of course be the size of the UIView upon which you drew. The update is needed, beacuse it seems that you may need to increase your drawing area size as you do it.
I have a UIView subclass object that animates and therefore changes its position over time as a subview in my UIViewController's view. Actually my moving UIView subclass is just an image of a ball and it's moving as if it was hanging down from my devices screens top border. But to be a real pendulum I'd like to also draw a line between my ball and the CGPoint it hangs down from on top of my screen.
My idea was to just draw a line every time the UIView changes its position. But as the moving is done within an iOS API (I'm just calling something like [myBallView swing]) I can't do this at the same place the movement is happening. I'm actually not animating the view myself.
The only solution I have in my mind to solve my issue is pretty bad: subclassing UIView, adding it as a superview to my moving UIView and adding a line every time drawRect is called. But I'm not even sure drawRect is going to be called there. Either way, there must be a better solution for this.
Does anyone know a good solution to my problem?
Making a custom subclass of UIView as the superview is reasonable.
However, rather than drawing the line yourself, I would suggest implementing +layerClass in your custom view, and making the backing layer a CAShapeLayer.
Then you can create a CGPath in the view's shape layer that is a line, and manipulate the start and end points of the line when your other view's center moves. Shape layers are designed to draw as part of the screen update process, and you could even make the change animate by changing the endpoints of the path with a CABasicAnimation.
I need to continuously change the color of all the lines I draw in a UIView. And I drew all the lines within the drawrect method to assign colors. Now I need to animation and change the colors as long as the view is shown. Is there a way to do it? Thanks a lot!
Look into using one instance of CAShapeLayer for each of the lines, instead of drawing them in drawRect:. You can animate changes to each layer's path and strokeColor properties as necessary.
EDIT
If you're just drawing horizontal lines, you may be better off simply using a UIView for each. Set the backgroundColor instead of implementing drawRect:. You can animate both the frame property and the backgroundColor property using +[UIView animateWithDuration:animations:] (or the more powerful variants of it). This is generally simpler than messing around with Core Animation.
Live and learn. Animations are better done outside of -drawRect:
Change your "lines" to individual UIView instances added as (preferably opaque) subviews to your view. You'd then set the appropriate backgroundColor on the view.
You can then use a simple UIView animation to animate them.
I have a UIScrollView over an image at the bottom of my app that acts as a dock with icons that can be scrolled through horizontally. Instead of the harsh edges of the UIScrollView, I would like the icons to fade out for a more aesthetically pleasing look. Being new to iOS development, I don't know if either of these would be valid options:
Create a faded image to use as an overlay on the scrollview so the
icons only appear through the visible portion.
Actually change the
alpha of the images based on their distance from the center (or from
each edge).
I suspect the first idea would be the most simple, but I'd like to throw this out there for any other ideas.
Note: I did see this tutorial, however that technique assumes that the background is a solid color. If I were to do it programatically, I would probably need to fade the individual images.
You can definitely implement something along the lines of #2. It'd be something similar to what the tutorial describes. The alpha transition however won't be as smooth as using the gradient layer mentioned in the tutorial or using an image since the entire icon would have the same alpha. How much discernible the difference is depends on the size of your icons. Smaller icons, very few will be able to tell the difference. Larger icons the difference would be quite clear.
You'd have to implement the
(void)scrollViewDidScroll:(UIScrollView *)scrollView
method in your scroll view's delegate class. This method will get called every time the scroll view changes the location of its content. In this method you can call its subviews and adjust their alphas as required. To optimize it a bit instead of calling the alpha adjustments on all the elements you can just update the subviews which are still partially/completely visible.
EDIT: to figure out which views to adjust you'll use the contentOffset property of the scrollView that gets passed as a parameter in the above method.