When Do You Need To Reset the view.frame Property After a Transformation iOS
I'm making a horizontal table view like the Pulse news reader. I've found several examples online and have it working, but am wondering when we need to set the view.frame property after a transformation.
The examples I've found reset the frame of the horizontal table view within the vertical table view cell after the 90 degree rotation
self.tableViewCell.horizontalTableView.transform = rotateTable;
self.tableViewCell.horizontalTableView.frame = CGRectMake(开发者_如何学运维0, 0, self.tableViewCell.horizontalTableView.frame.size.width, self.tableViewCell.horizontalTableView.frame.size.height);
More Context:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TableViewCell *cell = (TableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
self.tableViewCell.horizontalTableView.transform = rotateTable;
self.tableViewCell.horizontalTableView.frame = CGRectMake(0, 0, self.tableViewCell.horizontalTableView.frame.size.width, self.tableViewCell.horizontalTableView.frame.size.height);
self.tableViewCell.contentArray = [self.arrays objectAtIndex:indexPath.section];
self.tableViewCell.horizontalTableView.allowsSelection = YES;
cell = self.tableViewCell;
self.tableViewCell = nil;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
But don't reset the frame of the horizontal table cell after the cell's transformation (rotation):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"GameTableViewCell" owner:self options:nil];
cell = self.gameTableCell;
self.gameTableCell = nil;
}
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
cell.transform = rotateImage;
return cell;
}
I tried resetting the cell's frame and it had no effect on the output, even if I supplied
cell.frame = CGMakeRect(200, 200, cell.frame.size.height, cell.frame.size.width)
Which should have moved the cell around the Table View, no?
If I don't reset the frame of self.tableViewCell.horizontalTableView.frame
the horizontal table is rotated, but in the wrong location.
Why is it that I need to reset the frame of the horizontal Table View after rotating it, but not the individual cells (which are also rotated)?
Thanks!
// illustration by iPortable
ahh ok after reading it again I understand what you're asking ^^
- Setting the frame results in redrawing the view. This needs to be done because otherwise the old graphics may interfere and it looks ugly. The redraw should sharpen the draw lines too.
- you set your cell's frame to be positioned somewhere else (here 200,200) but it is completely normal that it has no effect. A cell cannot be elsewhere than
CGPointZero
. If you like to displace it, you have to create a bigger view and move the content. (the view can be translucent but it will slow down the scrolling on older devices dramatically) - you ask why you don't have to redraw each cells but only the tableView. Actually I don't know this exactly but it should be in fact of the frame change of the table view. Normally a frame change occur by rotating the device so that the width changes. Now it would be pretty stupid for a developer to update each cell for the new size. Thanks to Apple the tableView calls the necessary update request for each cell which is visible on screen. Eventually the same methods are called for all views:
drawRect:
I have problems understanding all your problems but will do my best.
When you rotate the device, the table view size changes (the frame). So you have to set the new frame for the table view to accomplish the new "design" (stretching the table view to the whole width). This is not true for the cells, because they stay at the same size even after the rotation.
an example:
________
|______| -> a cell width: 50 height: 200
after rotation:
___
| |
| | -> still the same size, width: 50 height: 200
|_|
a CGAffineTransform
is only a visual effect. It has no impact on the size neither the position of its subviews. So you have to do the exact opposite rotation for your cells then for your table view. In which direction is it wrong rotated? upside-down, left, right, or do you use an edge for rotating so that the rotation point is wrong chosen?
精彩评论