最近iphone需求要做到一些动画,想让一个图片围绕frame外的一个中心点进行旋转,但无论用center还是用anchorPoint,都要不到我想要的效果,只能采用此“小手段”来实现。如果有人有更好的实现方式,欢迎留言、评论、拍砖!
@begin
UIView *bgVw = [[UIView alloc] initWithFrame:CGRectMake(0, 30, 300, 300)]; bgVw.backgroundColor = [UIColor clearColor]; UIView *centerVw = [[UIView alloc] initWithFrame:CGRectMake(100, 130, 100, 100)]; centerVw.backgroundColor = [UIColor blueColor]; [self.view addSubview:centerVw]; UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 125, 50, 50)]; vw.backgroundColor = [UIColor brownColor]; UIView *vw2 = [[UIView alloc] initWithFrame:CGRectMake(250, 125, 50, 50)]; vw2.backgroundColor = [UIColor redColor]; UIView *vw3 = [[UIView alloc] initWithFrame:CGRectMake(125, 0, 50, 50)]; vw3.backgroundColor = [UIColor orangeColor]; UIView *vw4 = [[UIView alloc] initWithFrame:CGRectMake(125, 250, 50, 50)]; vw4.backgroundColor = [UIColor greenColor]; [bgVw addSubview:vw]; [bgVw addSubview:vw2]; [bgVw addSubview:vw3]; [bgVw addSubview:vw4]; [self.view addSubview:bgVw]; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0f]; [UIView setAnimationRepeatAutoreverses:YES]; [UIView setAnimationRepeatCount:10]; bgVw.layer.anchorPoint = CGPointMake(0.5, 0.5); bgVw.transform = CGAffineTransformMakeRotation([self radians:-180]); [UIView commitAnimations];
@end
昨天看了sina微博,看到cell右下角的图片,就想到思路应该是clip+rotate,今天随手测试,确实如此,果断上code:
UIView *containView=[[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 80)];
containView.backgroundColor=[UIColor redColor];
containView.layer.cornerRadius=10;
containView.clipsToBounds=YES;
[window addSubview:containView];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake((200-80), (80-40), 100, 60);
[btn setBackgroundImage:[UIImage imageNamed:@"touxiang.jpg"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
btn.layer.transform=CATransform3DMakeRotation(30.0*M_PI/180.0, 0, 0, 1);
[containView addSubview:btn];
还是很简单的,就两个地方:
1.containView.clipsToBounds=YES;当subviews的size超出superview的范围时,就把超出的那部分剪切掉;另外,在NO的情况下,超出的那部分是不能响应touchUpInside,不知为何...
2.btn.layer.transform=CATransform3DMakeRotation(30.0*M_PI/180.0, 0, 0, 1);这个简单地在Z上变换一下而已,细节参考文档
效果如下:
对于java的参数传递到底是值传递还是引用传递,一直是一个很疑惑的概念,到现在为止还是不怎么清楚,不过对于StringBuffer 和String类的参数传递也是一知半解。现在就做一个简单的例子测试
public class testBuffer { public static void main(String[] args) { StringBuffer a = new StringBuffer("A"); StringBuffer b = new StringBuffer("B"); operate(a,b); String s1="A"; String s2="B"; operate(s1,s2); System.out.println(a + " " + b);//1 System.out.println(s1 + " " + s2);//1 } private static void operate(StringBuffer a, StringBuffer b) { a.append(b); b = a;//2 } private static void operate(String a, String b) { a=a+b; b = a;//2 } }
运行结果为:
AB B
A B
也就是说当参数为StringBuffer时,当两个参数为A B时,得到的结果为AB B
党参数为String,两个参数为A B时,得到的结果为A B。
我也不知道这个到底是什么原因,只能死记