您的位置:首页 > 产品设计 > UI/UE

移动uiView 缩放uiView+一个View实现不同子view的功能

2013-12-09 22:40 555 查看
//移动uiView
UITouch *touch1=[touches anyObject];
CGPoint onePoint=[touch1 locationInView:self.window];
CGPoint newOnePoint=[touch1 previousLocationInView:self.window];
CGFloat distanceX=onePoint.x-newOnePoint.x;
CGFloat distanceY=onePoint.y-newOnePoint.y;
self.center=CGPointMake(self.center.x+distanceX,self.center.y+distanceY);
//显示uiview移动的方向
if (onePoint.x<newOnePoint.x) {
NSLog(@"向左移动");
}else if (onePoint.x>newOnePoint.x){
NSLog(@"向右移动");
}else if (onePoint.y>newOnePoint.y){
NSLog(@"向下移动");
}else if(onePoint.y<newOnePoint.y){
NSLog(@"向上移动");
}
//缩放uiView
if (touches.count>=2) {
//获取每一个touches的x,y值
NSArray *array=[touches allObjects];
CGPoint point1=[[array objectAtIndex:0] locationInView:self];
CGPoint point2=[[array objectAtIndex:1] locationInView:self];
CGPoint newPoint1=[[array objectAtIndex:0] previousLocationInView:self];
CGPoint newPoint2=[[array objectAtIndex:1] previousLocationInView:self];
//判断是不是同一个点并根据center缩放大小
if (point1.x!=point2.x && point1.y!=point2.y) {
CGFloat num=sqrt(((point2.x-point1.x)*(point2.x-point1.x))+((point2.y-point1.y)*(point2.y-point1.y)));
CGFloat newNum=sqrt(((newPoint2.x-newPoint1.x)*(newPoint2.x-newPoint1.x))+((newPoint2.y-newPoint1.y)*(newPoint2.y-newPoint1.y)));
self.bounds=CGRectMake(0, 0,self.bounds.size.width*(num/newNum),self.bounds.size.height*(num/newNum));
}
}

RootViewController
DragView *dragView=[[DragView alloc]initWithFrame:CGRectMake(30, 30, 130, 140)];
dragView.backgroundColor=[UIColor redColor];
[self.view addSubview:dragView];

//4.创建一个TouchView(kongjian),创建TouchView的3个实例,touchView1
被点击之后,touchView换背景颜色.tuochView2被点击后换位置(随机cengter),touchView3被点击后,打印一句话

if ([self viewWithTag:100]) {
self.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green: arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}else if ([self viewWithTag:200])
{
self.center=CGPointMake(arc4random()%100+30, arc4random()%400+40);
}else if ([self viewWithTag:300])
{
NSLog(@"minamima");
}

RootViewContrller文件

TouchView *touchViewColor=[[TouchView alloc]initWithFrame:CGRectMake(30, 30, 60, 30)];
touchViewColor.backgroundColor=[UIColor redColor];
[self.view addSubview:touchViewColor];
touchViewColor.tag=100;

TouchView *touchViewText=[[TouchView alloc]initWithFrame:CGRectMake(30, 200, 60, 30)];
touchViewText.backgroundColor=[UIColor redColor];
[self.view addSubview:touchViewText];
touchViewText.tag=300;

TouchView *touchViewCenter=[[TouchView alloc]initWithFrame:CGRectMake(30, 120, 60, 30)];
touchViewCenter.backgroundColor=[UIColor redColor];
[self.view addSubview:touchViewCenter];
touchViewCenter.tag=200;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐