Rotating UIView and still seeing the old position of the view - iphone

I am rotating a view using src.transform = CGAffineTransformMakeRotation(M_PI/2);, it is working well but I am still seeing the old position of the view (like that: http://cl.ly/233y403c2C1C451r1f28).
How can I refresh the view ? The view is in a UITableViewCell, I tried to refresh the row of the cell or even the tableview but it doesn't work.
Here is my drawRect method :
if (nil != _image) {
[_image drawInRect:rect contentMode:self.contentMode];
} else {
[_defaultImage drawInRect:rect contentMode:self.contentMode];
}
The view that I am rotating is embedded in a UIImageView which is part of the cell.
Thanks,
Martin

It sounds like you might have two copies of your image view there. When you rotate the top one, the bottom one is exposed.

What is the background colour of the UIView you're overriding drawRect in? Before drawRect is called, the background colour is applied to the context. Ensure you have a background colour set (i.e. not set to [UIColor clearColor], for example).
Also, what's the opaque property of your UIView set to?

If figured it out. I was trying to rotate the image while I was downloading it. When I rotate it from a delegate callback that is call when the image is loaded everything works fine.
All of that makes perfect sense !

Related

UIPresentModalViewController view alpha is not being set

I want to present a view controller where I have one Background imageView.
Alpha for that imageview is 0.5 (want to add as semi-transperant black image so). But when I present that view controller that alpha doesn't work for view. That image entirely looks blackish, like alpha has not been even set.
This issue is there for iPad device only.
Please help me out.
Code:
ViewController1.m:
[self presentModalViewController:viewController2];
ViewController2.xib: (in nib I am setting below values no in code)
[self.view setBackgroundColor:[UIColor clearColor]];
[self.bgImageView setAlpha:0.5]; // this image is dark black, i want to display the
content of the screen which is behind this (viewController1.view), kind of semi-transperancy
I tried one more thing, this time i have removed imageView and set uiview bgcolor to black, opaque=NO, alpha=0.2 (in nib itself). So while animation of presenting it looks perfect. But when view has been placed it turns into alpha=1.0 (complete black)
Still there is no transparency where am i wrong over here.
Answer Is Here: There is some bug/limitation with ModalViewController so its better to go with addSubview for such situation
Try to write imageview.alpha = 0.5 after you present the modelviewcontroller and see what happens. Just give it a try.
EDIT:
1)Clean Build and Run
The image alpha you are trying to set, is it in viewcontroller that you are presenting from or is it in modalviewcontroller?
Answer Is Here: There is some bug/limitation with ModalViewController so its better to go with addSubview for such situation

Custom UITableViewCell corners distorted on orientation change (UACellBackgroundView)

I'm trying out this nice way of customizing grouped UITableViewCell backgrounds:
http://code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds
I've implemented it in a test app and it works great, except for one thing... when rotating the device, the cell background is stretched, making the corners look distorted.
Is there any way to force a table cell to redraw itself on an orientation change? Do I need to use setNeedsDisplay somewhere?
(I've tried [cell.backgroundView setNeedsDisplay] in a couple of places just as a random guess... but that didn't work.)
Set backgroundView.contentMode = UIViewContentModeRedraw if you want it to redraw when the bounds change.
Additionally, do not override -isOpaque like the example suggests; I'm pretty sure that opaque-ness is backed by the CALayer property of the same name. Instead, set self.opaque = NO in initWithFrame:.

How to scroll the content without scrolling the image in the background using UIScrollView?

I am trying to scroll the content without scrolling the image in the background using UIScrollView.
I am using IB and setting the FileOwner View to point to the Scroll View (Image View is a child of the Scroll view). I have made the image height to be 960 pixels.
I have also set scrolling content size in the vierController that owns this UIView
(void)viewDidLoad {
UIScrollView *tempScrollView = (UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 960);
}
My problem is that the Image only appears moves along with the content.
I have tried taking out the settings in viewDidLoad, but the scrolling cease to function.
I have also tried changing the location of the image and have it placed under VIEW instead of Scoll View (by the way Scroll View is a child of VIEW), but that resulted in the app breaking (termination error).
Any advice would be appreciated.
The easiest (and correct way) is to set an background image to your UIScrollView
UIImage *img = [UIImage imageNamed:#"image.png"];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
The easiest way is to set an UIImageView behind your UIScrollView(UIImageView and UIScrollView under UIView) and make your UIScrollView background color as clear color as mentioned by Janson. By this way you can scroll the content without scrolling the image in the background using UIScrollView.
Thanks. This was extremely helpful.
The one thing that I would add (pardon if it is obvious, but for people new to the platform (i.e., me), it took a bit of playing around).
I put the code from "unset"'s example after where I setup the contentSize=CGSizeMake in the viewDidLoad of the UIViewController , i.e.,:
// Setup the Scroll View
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 720);
// Set Stationary Background, so that while the user scroll the background is
// fixed.
UIImage *img = [UIImage imageNamed:#"bg-body.jpg"];
[tempScrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
Then, I went into the ViewController .xib file and on the UIView that is inside of the UIScrollView, I set the background color to "Clear Color".
I sure that there is a way to do this through code at the same time as setting the background of the UIScrollView, but at least that is how you can do it through IB.
Jason

Black Corners On Grouped UITableViewCells Only After Navigation Pops

I am no graphics expert but I somehow managed to make some good looking custom grouped UITableViewCells by setting the background view to a backgroundView with some CG code. In all SDK's up to 3.1.3 (maybe 3.2... I haven't tested on the iPad) this was working great, but I think a more recent SDK has introduced a change in the way graphics are cached offscreen.
Upon first render, everything is great: The drawing is fine and the corners are transparent. If I push a couple of view controllers on the navigation stack and come back, there are now black corners that appear in the views:
BEFORE && AFTER
(source: tinygrab.com)
   
(source: tinygrab.com)
I have tons of code, most of which is written up here. I have tried tweaking to the best of my ability, looking at the docs for applicable changes, but after at least 8 hours in I still cannot find what might cause this. I have tried setting every view I can think of to be backgroundColor=clearColor and opaque=NO What else am I missing? Any debugging tips?
UPDATE:
I have some debug code in viewDidAppear that prints the backgroundColor and class description of all the subviews.
- (void)debugView:(UIView *)view {
DebugLog(#"%# - %#", view.backgroundColor, [[view class] description]);
for (UIView* child in view.subviews) {
[self debugView:child];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[DownloadController.networkQueue setSuspended:NO];
for (TTTableViewCell *cell in [self.tableView visibleCells]) {
[cell debugView:cell];
}
}
With this code, I inspect the backgroundColor settings of the cell views on first load, when it is fine, and then again after coming back. There are some differences, but all the colors are still set to be clear. This leads me to believe the issue is underneath the UITableViewCell.
UPDATE 2:
I have created a simple sample application to highlight the problem.
I have tested the sample application and can reproduce the black corners issue.
After some experiments, it seems that the black corners issue is related to the caching of the layers used to render the table view. The geometry of the cell's layer seems to be important:
On the first paint, the cell is asked to be painted into a rect. Your code is painting a rounded path, but clipped out the corners. As the underlying tableview is already drawn, no problem occurs. The rect zone is cached, with its corners unpainted.
When the controller is pushed, a cached image is stored, with rectangular placeholders for the cells.
When the controller is popped, the cached image and the cells are drawn. But the place to draw cells is rectangular but the cell's cached image is not, leading to black corners.
In order to get rid of the black corners you can:
Make sure that all the cell's rect is painted. This means using the same color to file the cell before drawing the edge as the tableview's background color. If your tableview use the default background color, you can use [UIColor groupTableViewBackgroundColor].CGColor as filling color; it is a pattern based color and follows device orientation (yeah); but the painting is not perfectly aligned with the background (damn).
Use a CALayer mask on the cell's layer. This implies creating a mask CGImage, set it as the layer's content and assign the mask layer to the cell's layer. Not sure about the performance.
Hope it helps a bit.
Update
After some unsuccessful attempts, I dropped the mask idea because it was too clumsy.
I have re-read the code for the cell's layer and found out a way to remove the black corners in a simple way. The basic idea is that a CAGradientLayer is fully transparent only if its gradient colors are clear. By using the following display method, the black corners went away (both on the simulator and on the device):
- (void)display {
if (_override) {
self.colors =
[NSArray arrayWithObjects:
(id)[UIColor colorWithRed:colorComponents[0] green:colorComponents[1] blue:colorComponents[2] alpha:colorComponents[3]].CGColor,
(id)[UIColor colorWithRed:colorComponents[4] green:colorComponents[5] blue:colorComponents[6] alpha:colorComponents[7]].CGColor,
nil];
} else {
self.colors =
[NSArray arrayWithObjects:
(id)[UIColor clearColor].CGColor,
(id)[UIColor clearColor].CGColor,
nil];
}
[super display];
}
Of course, this could be optimized a bit:
Create the color arrays once.
Provide a custom setter for override property that change the layer's colors.
Remove the display method as it is no needed anymore.
I experimented with your sample code using OS3.2 on the simulator and it definitely shows the same symptom. I tried a few things and ended up with this fix:
self.cornerRadius = 12;
Add this into UAGradientCellBackgroundLayer, in init method.
Throwing this out there (perhaps it will help), adding the following to cellForRowAtIndexPath has an interesting effect:
cell.backgroundView.alpha = 0.4;
cell.selectedBackgroundView.alpha = 0.4;
Not all of the cells have the black background. Changing the alpha seems to change the probability that any given cell will improperly render. alpha = 1.0 guarantees a black background, whereas any lower decreases the probability of this effect.
The black background also fades, so I'm sure you all knew, but yeah, it has to do with the UAGradientCellBackgroundView.
Good luck!

How do I draw to the contentView of a UITableViewCell?

I'm trying to draw a line on a custom UITableViewCell. When I draw inside the overridden drawRect and use the current graphics context I don't see anything.
I know I can't see anything because the draw rect is drawing to the UIView and not the contentView. So my question is, how do I draw to the content view using CG? Say, grabbing the graphics context to the contentView?
I have played with this in the past and what i have ended up doing is creating my own subclass of UIView and adding iot to a custom subclass of UITableViewCell and drawing to it that way. I like the control that this gives me.
For some reason (I don't know why, but I did notice this) if you "custom-draw" (ie, redefine drawRect) on what's in the 'contentView' of your cell, it does not show up on the table cell. However, the view that you assign to cell.backgroundView and cell.selectedBackgroundView show up perfectly well...
You could put your custom view in cell.backgroundView, put nothing in cell.contentView and it will show up fine (except for when you select the row, because then your view disappears and is replaced by cell.selectedBackgroundView).
So, what you can do is:
Use cell.contentView to show a custom view without any background
Use cell.backgroundView and cell.selectedBackgroundView to show a fancy background for your cell...
Or:
Make your custom view flexible enough so that it can show both the selected and non-selected state, and use 2 instances of your custom view: one in cell.backgroundView and one in cell.selectedBackgroundView
I found my answer here. Basically the super class ABTabeViewCell sets up the context so you can easily draw in the drawContentView function.