开发者

how to invert transform a UIView?

I am using core-plot library to draw a bar-chart on my iPhone application and the problem arises when I try to add a label or some other views to the graph,

Actually the view I added is drawn vertically invert like this ........

The code is like

UILabel *lbl= [[UILabel alloc] initWithFrame:CGRectMake(250, 90, 70, 25)];
    [lbl setBackgr开发者_高级运维oundColor:[UIColor redColor]];
    [lbl setText:@"HELLO"];
    [self.view addSubview:lbl];
    [lbl release];

I am not daring to play with core-plot library.

So is there any other way to do the things right? should I do a transform before adding the views?

If this is the solution then this will be costly because I have to add more than one subview.

Hoping my question is clear to everybody.


Well, it looks like the core-plot view is inverted using a CGAffineTransform. Your view is probably inheriting the inversion from its superview. It's actually quite simple to do,

lbl.transform = CGAffineTransformMakeScale(1,-1);

Should do the trick. If you have to do it many times perhaps simplify it with something like,

CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1);
lbl.transform = verticalFlip;
otherLbl.transform = verticalFlip;

etc...

An x-axis flip would be CGAffineTransformMakeScale(-1,1);


To avoid guessing actual transform values you can try to use CGAffineTransformInvert function:

lbl.transform = CGAffineTransformInvert(self.view.transform);


If we want to transform a layer which is 3D model you need to handle it as follows,

Swift 5:

// To inverse the graph vertically
annotationGraph.transform = CATransform3DMakeAffineTransform(CGAffineTransform(scaleX: 1, y: -1))

otherwise you would have receive this,

Cannot assign value of type 'CGAffineTransform' to type 'CATransform3D'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜