Using CATransform3D

半世苍凉 提交于 2019-12-11 10:57:02

问题


I'm trying to make a rotation on a tableview to tilt the table (to give the effect of a 3d text crawl similar to the star wars opening crawl).

After looking around I found this question objective-с CALayer UIView real rotation

and the accepted answer seems to do what I want, however when I apply the code to my TableView it does nothing and the table appears as usual.

This is the code I am copying:

float distance = 50;

CATransform3D basicTrans = CATransform3DIdentity;
basicTrans.m34 = 1.0 / -distance;
_tableView.layer.transform = CATransform3DRotate(basicTrans, M_PI_4, 1.0f, 0.0f, 0.0f);

I'm placing this in my viewDidLoad method after creating my array of Strings (that populate the tableView)

I currently only have three other methods in the Controller:

didReceiveMemoryWarning (automatically addd when project created)

tableView: numberOfRowsInSelection (used for setting up the table view) tableView: cellForRowAtIndexPath (used for setting up the table view and setting the cells text form the array)

My understanding is that the tableview has a CALayer, and that the CATransform3D manipulates this to give the representation of the view in a 3d space. If my understanding is correct then I don't get why the list is shown normally on screen? I appreciate the numbers my not give the effect I want yet but they should at lest effect the appearance of the tableView on screen.

Also I have imported QuartzCore etc and added it in linked frameworks


Solution is to use the code marked as OLD answer in the the - (UITableViewCell *)tableView: cellForRowAtIndexPath: method after the cell is checked for being null.


回答1:


Since the approach suggested below has not worked, another thing I would try out is applying the transform to the UITableView's subviews. Actually, UITableView is a UIScrollView, so it is just a container for subviews that make up the real content of the table view. I would try something like this:

for (UIView* subview in tableView.subviews) {
  subview.layer.transform = ...;
}

I have never inspected a table view subviews hierarchy, so I cannot say whether this will work or you should rather apply the transform to just one of the subviews, but I hope this can lead you in the right direction.

OLD ANSWER:

You could try setting your table view's layer sublayerTransform instead of `transform':

You typically use this property to add perspective and other viewing effects to embedded layers. You add perspective by setting the sublayer transform to the desired projection matrix. The default value of this property is the identity transform.

(source).

I am suggesting this based on the hypothesis that a UITableView has quite a complex structure in terms of subviews, so transforming just the view's layer might have no effect. I haven't tried it, though, so I cannot guarantee it will work.



来源:https://stackoverflow.com/questions/14675440/using-catransform3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!