UI基础整理-5

来源:互联网 发布:南京浦口区网络问政 编辑:程序博客网 时间:2024/06/03 21:45

target_action

重写addtarget: action:方法

让_target来执行_action[button addTarget:self action:@selector(test:)];

imageView

//ios默认支持的图片格式是png,因为在较小尺寸下,png的体积更小,方便存储.
   //苹果官方建议,APP当中,图片大小应该不大于1024*1024,以免加重APP运行负荷.

//创建图片对象
   
//根据图片名字直接获取图片对象
//    UIImage *image = [UIImage imageNamed:@"b.jpg"];
   
//根据图片路径来获取图片对象
    UIImage *image =[UIImageimageWithContentsOfFile:@"/Users/lanou3g/Desktop/cc.png"];

   //创建图片视图
   
//    //公司中一般使用这种
//    //直接使用图片来创建图片视图,可以保留原尺寸比例(视图大小和图片大小一致)
//    UIImageView *imgView = [[UIImageView alloc]initWithImage:image];
//    [self.view addSubview:imgView];

 //经常使用的方法
   
UIImageView *imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,290,290)];
    imgView.
center=self.view.center;

   
//设置图片
    imgView.
image= image;
   
//调整显示内容为原比例(不拉伸)    UIViewContentModeScaleAspectFit:正常显示
//    imgView.contentMode = UIViewContentModeScaleAspectFit;
   
//添加视图
   
//切圆角
    imgView.
layer.cornerRadius=145;
    imgView.
layer.masksToBounds=YES;
   
   
//图片视图的用户交互是默认关闭的
   
//NSLog(@"%d",imgView.userInteractionEnabled);
   
   
   
//开启视图的用户交互
    imgView.
userInteractionEnabled=YES;//用户交互默认是关闭的
    [self.viewaddSubview:imgView];


方式一:根据图片名字直接获取图片
    UIImage *image = [UIImage imageNamed:@“1.png”];

 方式二:根据图片路径来获取图片

   UIImage *image = [UIImage imageWithContentsOfFile:@"/Users/lanou3g/Desktop/n.jpg"];

手势

从名字上我们就能知道, Tap(点击)、Pinch(捏合)、Rotation(旋转)、Swipe(滑动,快速移动,是用于监测滑动的方向的)、Pan (拖移,慢速移动,是用于监测偏移的量的)以及 LongPress(长按)。

轻拍手势UITapGeatureRecognizer

长按手势UILonngPressGestureRecongnizer

轻扫手势UISwipeGestureRecognizer

旋转手势UIRorationGestureRecognizer

捏合手势UIPinchGestureRecognizer

平移手势UIPanGesturoRecognizer

屏幕边缘轻扫手势UIScreecEdgePanGestureRecognizer

//    //轻拍手势
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//    //触发轻拍收手势需要的点击次数
//    tap.numberOfTapsRequired = 1;
//    //触发轻拍手势需要的手指个数(在模拟器中按住option在点鼠标能出来两个手指)
//    tap.numberOfTouchesRequired = 2;
//   
//   
//    //添加手势(注意:不是addSubview)
//    [imgView addGestureRecognizer:tap];
   
   
   
   
//    //长按手势
//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
//    [imgView addGestureRecognizer:longPress];
//    //最短按压时间()
//    longPress.minimumPressDuration = 3600;

   
   
//    //轻扫手势
//    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction:)];
//    //轻扫手势默认设置的方向为向右滑
//    swip.direction = UISwipeGestureRecognizerDirectionLeft;//修改为往左滑
//    [imgView addGestureRecognizer:swip];

//    //平移手势
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//    [imgView addGestureRecognizer:pan];

//    //旋转手势
//    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
//    [imgView addGestureRecognizer:rotation];

   
//    //缩放手势
//    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
//    [imgView addGestureRecognizer:pinch];

   
//屏幕边缘轻扫手势
   
UIScreenEdgePanGestureRecognizer *screenEdge =[[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(screenEdge:)];
    screenEdge.
edges=UIRectEdgeLeft;
    [
self.viewaddGestureRecognizer:screenEdge];
 
   self.view.backgroundColor= [UIColormagentaColor];

以下是关于屏幕的操作:
#pragma mark ----------屏幕边缘轻扫手势--------
-(void)screenEdge:(UIScreenEdgePanGestureRecognizer*)screenEdgePan{
    //触摸屏幕左侧会变色
   
self.view.backgroundColor= [UIColorcolorWithRed:arc4random()%256/255.0green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}



#pragma mark ----------缩放手势--------
-(void)pinchAction:(UIPinchGestureRecognizer*)pinch{
   
//    //六个参数(会回到原点)
//    pinch.view.transform = CGAffineTransformMake(pinch.scale, 0, 0, pinch.scale, 0, 0);
   
//    //三个参数(不会回到原点)
//    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
//    pinch.scale = 1;//如果这边不加的话,会缩放的很快,pinch.scale<0是只会缩小,pinch.scale>0是只会放大


   
//两个参数(会回到原点)
    //    pinch.view.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
}



#pragma mark ----------旋转手势--------
-(void)rotationAction:(UIRotationGestureRecognizer*)rotation{
   
//    //六个参数
//    rotation.view.transform = CGAffineTransformMake(cos(rotation.rotation), sin(rotation.rotation), -sin(rotation.rotation), cos(rotation.rotation), 0, 0);
   
//    //两个参数
//    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
//    rotation.rotation = 0;//如果不加这句话,会转的特别快


   
//一个参数
   //rotation.view.transform = CGAffineTransformMakeRotation(rotation.rotation);   
}


#pragma mark ----------平移手势--------
-(void)panAction:(UIPanGestureRecognizer*)pan{
   
   
//获取手势操作的点
   
CGPoint point= [pan translationInView:pan.view];

//    //六个参数(会跳回原点)
//    pan.view.transform = CGAffineTransformMake(1, 0, 0, 1, point.x, point.y);

//    //三个参数
//    //pan.view.transform(会记住变换的位置)
//    pan.view.transform= CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
//    //矫正位置
//    [pan setTranslation:CGPointZero inView:pan.view];

   
   //两个参数(还是会跳回原点)
   //pan.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
//    NSLog(@"%@",pan.view);
}



#pragma mark ----------轻扫手势--------
-(void)swipAction:(UISwipeGestureRecognizer*)swip{
   
   
NSLog(@"...");
}



#pragma mark ----------长按手势--------
-(void)longPressAction:(UILongPressGestureRecognizer*)longPress{
   
   
NSLog(@"长有本事别放开...");
}


#pragma mark ----------轻拍手势--------
-(void)tapAction:(UITapGestureRecognizer*)tap{
   
   
NSLog(@"拍一拍");
}








0 0
原创粉丝点击