How would I go about drawing a view offscreen in order to perform some calculation before the view's draw is actually done? Can this be done by drawing to a temporary layer?
I am not getting your exact problem... But you can use
-(void)viewWillAppear:(BOOL)animated
this method will be called every time before your view will appear on the screen... you can do the calculation and make some changes here before view appear on the screen...
Related
I have a tableView. Instead of having multiple subviews in each tableViewCell , i have added one main subview which draws each of the other subviews in its drawRect method.
(I have read somewhere that this makes scroll animation look better. Also apple has a sample project CustomTableViewCell).
First , problem is when i changes the orientation of device, then drawRect does not call automatically. And each tableviewcell appears to be stretced.
I solved this by using
mainView.contentMode = UIViewContentModeRedraw
This solves the problem. Now when the orientation change is done, drawRect is called automatically .
But during the orientation change, the animation of rotating view still shows each tableview cell stretched. It is a very small thing, but still it is noticeable. Can anyone suggest something on this ?????
You can't redraw during animation (not easily, anyway).
Split the table cell into components again and setup autoresizing correctly. The animation will then animate only component positions and it will look fine.
Where did you read drawRect: would make scroll animation better? Did you have some problems with scrolling animation?
Edit:
IMHO There is no way how to fix resizing animations and keep the performance increase from using drawRect: instead of subviews.
I am displaying a big image in CATiledLAyer.
Now i want to draw a line between two points where the user touches on that image.
Would that be possible ?? , if so can you outline me the way to accomplish it ??
Thanks,
Ratna
I did something similar for an app a while back. The strategy I used was to place another view on top of the view with the tiles. Then set your self up as a UIScrollViewDelegate and everytime the tiled view scrolls or zooms, recalculate where the overlayed objects need to be. Just read the contentOffset value and the zoomScale and you should have what you need to correctly figure out where your overlay needs to be positioned. You will also have to make sure that touches are correctly reaching the tile view if they have to pass through your overlay view.
So I made a UIView subclass let's called it 'MyView' that simply draws a line inside the DrawRect method.
I then drag a UIImageView in interface builder and put it inside MyView. But now when I run the program it's obscuring the line that I'm drawing. I'm wondering, is there a way for me to draw the line on top of the image that I have dragged into my View?
No, you'll have to reorganise the view hierarchy. CoreGraphics uses a painters model so views behind will always be drawn over by views infront. To solve this you could use a container view that holds your image view and has a transparent view (line drawing view) that sits over the image view.
Another option is to draw the image using core graphics calls in your drawRect method and then draw the line over it.
One way is to have whatever adds MyView to add your UIImageView on top of MyView. It's not ideal (you probably have good reasons for keeping MyView and the UIImageView in a single class) but definitely solves your problem (I recently did this, only, I added it to the keyWindow =)
I would use a CAShapeLayer that has a path representing the line, and insert that layer ahead of the built-in view layer. Then it should draw on top.
I have a UIScrollView that contains a UIView onto which I am drawing a rather complex graphic using CGPaths. After the view finishes loading the graphic is distorted in that it is elongated horizontally and vertically. If I redraw it, it looks normal.
Any ideas on what is causing this and how to fix it?
When you initialise your UIView, use initWithFrame and set the frame to the correct size that it will be after everything is drawn, etc. If you are not sure here are some ideas:
Get the 'bounds' of the parent view.
Call layoutSubviews on the root view, then get its frame, then create your UIView and add it to its parent.
I've created a NIB file with some button controls on it, and as a background it has an ImageView which contains a PNG file loaded from my project.
What I want to do is to draw on top of the ImageView - imagine that my UI is a clockface and I want to draw the hands of the clock on top of the background image.
Is it the correct approach to try to subclass the UIImageView and use its DrawRect as when I use the DrawRect of the default view I don't see anything on the screen?
I'm a little lost as I'm finding this particular bit of the documentation hard to follow.
Thanks.
Create a new custom UIView (e.g. named HandsView) that sits on top of your background view (by adding it as a subview of the UIImageView). In the custom view, you can use the drawRect method. Make sure, you clear the context to transparent, so that the background image can be seen below the HandsView.
If you just want to draw turning hands, you can also try to use two fixed UIImageViews with the images of the hands and use the transform property to apply a rotation.