13-UIKit(tableviewcell贴图、手势GestureRecognizer、transform变形)
目录:
一、tableviewcell贴图
1.tableviewcell贴图
在storyboard中设置:
tableview的separator(分隔符)为none,既然要贴图默认分隔符就不要用了
tableviewcell的background为Clear Color,默认是白色,去掉才能看到效果
在代码中:
设置cell贴图backgroundView
也可以设置cell选中贴图selectedBackgroundView
cell.textLabel.text = @"hello world"; UIImage *image = [UIImage imageNamed:@"list.png"]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; cell.backgroundView = imageView; cell.backgroundColor = [UIColor clearColor]; // cell.textLabel.backgroundColor = [UIColor clearColor]; UIImage *selectedImage = [UIImage imageNamed:@"listSelected.png"]; UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:selectedImage]; cell.selectedBackgroundView = selectedImageView;// 选中贴图
2 颜色美化
2.1 tintColor
当一个控件没有设置自己的颜色时,会使用父视图的tintColor
批量设置界面中的颜色:
将一个VC中view的tintColor设置成指定颜色,导致view下所有的子视图的tintColor变成此颜色,除非某个子视图的tintColor被单独设置过。
UIWindow如果在AppDelegate设置了window的tintColor,那么整个应用的tintColor都会被设置,除非某个子视图的tintColor被单独设置过。
self.window.tintColor = [UIColor redColor];
2.2 UIAppearance
可以针对某一种类型的控件设置颜色,以及贴图,UIView类(子类)的对象都有一个属性:appearance,这个属性是一个特殊对象,对这个对象进行美化和设置机会影响到这一类视图的样子
// 设置所有的button [[UIButton appearance] setTintColor:[UIColor blackColor]]; [[UIButton appearance] setBackgroundImage:[UIImage imageNamed:@"delete_btn"] forState:UIControlStateNormal];
二、手势GestureRecognizer
1 基本概念
视图对照用户操作的一种判断,对用户触控屏幕的行为的一种包装
2 分类
一次性手势
Tap touch一下屏幕
Swipe 轻扫一下屏幕
连续性手势
LongPress 长按
Pinch 捏,扩
Pan 拖动
rotation 旋转
3 使用手势
1> 用在视图上,视图识别用户的手势,识别成功后会触发事件
2> 如何将手势加到视图上
创建手势对象
调用视图对象的addGestureRecognizer方法
4 手势的类型
每一个手势都有自己的类,这些具体的手势类继承自UIGestureRecognizer,具体的手势类命名方式如:UIXxxxGestureRecognizer
每一个具体的手势对象都有特定的属性,用来描述手势具体的参数
5 具体的手势对象
5.1 Tap手势(UITapGestureRecognizer)touch一下屏幕
用法:
1> 创建手势对象
2> 修改手势相关属性
3> 加入到指定的view中
Tap手势特定的属性有:
.numberOfTapsRequired 连续按几下触发事件
.numberOfTouchesRequired 最少多少个触点(手指头)触发
locationInView 获取点击位置
- (void)viewDidLoad { [super viewDidLoad]; // 1创建手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; // 2设置属性 tap.numberOfTapsRequired = ; // 连续按几下触发事件 tap.numberOfTouchesRequired = ; // 最少多少个触点(手指头)触发 // 3添加到视图 [self.view addGestureRecognizer:tap]; } -(void)tap:(UITapGestureRecognizer *)sender{ CGPoint point = [sender locationInView:self.view]; NSLog(@"%f,%f",point.x,point.y); }
5.2 Swipe手势(UISwipeGestureRecognizer)轻扫一下屏幕
.numberOfTouchesRequired最少多少个触点(手指头)触发
.direction 扫的方向,不能左右上下都有,可以设置左右或上下,或设置一个方向
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tap)]; swipe.numberOfTouchesRequired = ; //swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;// 方向 swipe.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;// 方向 [self.view addGestureRecognizer:swipe]; }
5.3 pinch手势(UIPinchGestureRecognizer)捏,扩
scale: 缩放比例,初始值为1
velocity: 速度,只读
这两个属性一般不需要设置,而是用于读取
是持续性手势,最大的特点就是在手势执行期间会连续调用方法
- (void)viewDidLoad { [super viewDidLoad]; UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; [self.view addGestureRecognizer:pinch]; } -(void)pinch:(UIPinchGestureRecognizer *)sender{ NSLog(@"%lf,%lf",sender.scale,sender.velocity);// scale 缩放比例 velocity 速度 if (sender.scale < 1.0 && sender.velocity < -5.0) { self.textField.hidden = YES; } if (sender.scale > 1.0 && sender.velocity > 15.0) { self.textField.hidden = NO; } }
5.4 longPress(UILongPressGestureRecognizer)长按
. minimumPressDuration 至少按下多少秒算长按,默认0.5
- (void)viewDidLoad { [super viewDidLoad]; UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; longPress.minimumPressDuration = 0.5; // 按下多少秒算长按,默认0.5 [self.view addGestureRecognizer:longPress]; }
5.5 Pan拖动
locationInView:UIView 来获取手势目前的位置在指定视图中的坐标,视图不同,坐标不同
经常用pan手势来移动指定的视图,移动时,所修改的是需要移动的视图的center属性
- (void)viewDidLoad { [super viewDidLoad]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self.view addGestureRecognizer:pan]; } -(void)pan:(UIPanGestureRecognizer *)sender{ CGPoint current = [sender locationInView:self.view];// 相对self.view托 CGPoint point = [sender locationInView:self.subView];// 相对self.subView托 NSLog(@"view(%f,%f)",current.x,current.y); NSLog(@"subView(%f,%f)",point.x,point.y); // center表示中心点处在父视图的哪个位置 self.playerView.center = current; CGPoint velocity = [sender velocityInView:self.view]; // 一秒走多少个像素 NSLog(@"velocity:%f,%f",velocity.x,velocity.y); }
5.6 Rotation(旋转)
.rotation 旋转的弧度(PI)
.velocity 旋转的速度,每秒多少弧度
- (void)viewDidLoad { [super viewDidLoad];
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; [self.view addGestureRecognizer:rotation]; } -(void)rotation:(UIRotationGestureRecognizer *)sender{ // rotation 旋转的弧度(PI) velocity 旋转的速度,每秒多少弧度
NSLog(@"%f,%f",sender.rotation,sender.velocity); // 拿出来
CGAffineTransform imageTransform = self.imageView.transform; // 改一改 旋转
imageTransform = CGAffineTransformRotate(imageTransform, sender.rotation); // 放进去
self.imageView.transform = imageTransform; sender.rotation = ; // self.imageView.transform = CGAffineTransformMakeRotation(sender.rotation);// 第二次旋转从0开始 }
6 transform变形
6.1 概念:对一个view中transform属性的改变,改变此属性会导致发生变形,旋转、缩放、位移
6.2 本质:一个view的transform属性描述的其实是此view在父视图中的变化,这个属性内部是由一个3行3列的矩阵组成。
6.3 能改变的是:视图的 旋转角度、缩放比例、位移。
6.4 如何使用:UIView.transform
注意:在修改transform属性时,需要将autolayout关掉(imageView的第一个检查器),否则自动布局可能会影响变形
修改transform属性:
CGAffineTransformRotate(transform,rotation)在transform的基础上叠加一个rotation的旋转
CGAffineTransformScale()叠加一个scale的缩放
CGAffineTransformTranslation()叠加一个位移
拿出来,改一改,放进去。
创建transform的方法:
CGAffineTransformMakeRotation
CGAffineTransformMakeScale
CGAffineTransformMakeTranslation
每次改变scale后,需要将scale恢复成初始值1.0
pinch.scale = 1.0
每次translation后,需要将只恢复成初始值:[pan setTranslation:CGPointZero inView:self.view];
获取用户拖动的位置
[pan translationView:xx];
手势的状态:
开始状态
改变状态
结束状态
7 练习:图片查看器
1)使用代码向view中增加一个UIImageView对象,UIImageView的大小和图片本身的大小要一致
2)使用center属性将UIImageView移动到屏幕的中心
3)使用transform将UIImageView缩放到屏幕刚刚能显示下所有的内容,要求不损失宽高比,
4)对UIImageView增加rotation手势识别,支持旋转,
5)增加pinch手势,支持缩放
6)增加pan手势,支持位移动
7)增加tap手势,双击后恢复原装(第三步)
8 多手势同时触发
默认情况下,一个视图中如果有多个手势,同一时刻只能触发一个,如果希望手势可以同时触发,那么手势对象就需要一个委托对象回答问题:两个手势对象是否可以一起触发
1> 让当前controller遵守协议UIGestureRecognizerDelegate协议
2> 实现方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
3> 将当前对象设置为各手势对象的委托
作业:
做一个视图类MXMessage,这个类的一个对象其实是一个聊天气泡,
MXMessageView:
-message:NSString
-formMe:BOOL
private:
-messageLabel:UILabel
-messagePopImageView:UIImageView
显示:
if(self.fromMe){
蓝色泡泡右上角,文字颜色是白色,大小根据字符串计算
}else{
灰色的泡泡,文字原色是深灰色,大小根据字符串计算
}
注意:
storyboard
一定是被storyboard创建
self.storyboard intina...创建VC
prepareSegue跳转前调用
13-UIKit(tableviewcell贴图、手势GestureRecognizer、transform变形)的更多相关文章
- transform(变形)和transform-origin(变形原点)
转载请说明出处,原文地址http://blog.sina.com.cn/s/blog_780a942701014xl8.html transform(变形)和transform-origin(变形原点 ...
- CSS transform(变形)和transform-origin(变形原点)
transform(变形)和transform-origin(变形原点)的说明: 目前这两个属性得到了除去ie以外各个主流浏览器webkit,firefox,opera的支持,属性名分别为 -webk ...
- CSS3 Transform变形理解与应用
CSS3 Transform变形理解与应用 Transform:对元素进行变形:Transition:对元素某个属性或多个属性的变化,进行控制(时间等),类似flash的补间动画.但只有两个关键贞.开 ...
- 【Unity】第13章 光照贴图和光影效果
分类:Unity.C#.VS2015 创建日期:2016-05-19 一.简介 在Unity 5中,Lighting是—种增强场景光照和阴影效果的技术,它可以通过较少的性能消耗使静态场景看上去更真实. ...
- hammer.js实现背景图手势缩放调整位置
<!DOCTYPE html> <html> <head> <script> function getxy(e){ var a=new Array() ...
- 程序猿的量化交易之路(13)--Cointrader类图(1)
转载须注明出处:http://blog.csdn.net/minimicall? viewmode=contents, htpp://cloudtrader.top 今天開始正式切入到Cointrad ...
- CSS3中的transform变形
在CSS3中,用Transform功能可以实现文字或图像的旋转.缩放.倾斜.移动这四种类型的变形,这四种变形分别使用rotate.scale.skew和translate这四种方法来实现.将这四种变形 ...
- CSS3 transform变形(3D转换)
一.三维坐标 空间中三维坐标如下图所示: 向上为-Y,向下为+Y,向左为-X,向右为+X,向前为+Z,向后为-Z. 二.perspective(n)为 3D 转换元素定义透视视图 perspectiv ...
- css 动画(一)transform 变形
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 有段时间我是没理清 transform.translate.transition 和 animation之 ...
随机推荐
- C++ 函数映射使用讲解
想想我们在遇到多语句分支时是不是首先想到的是 switc case 和 if else if ... 这2种方式在编码方面确实简单少,但是当分支达到一定数量后,特别是分支内部有嵌套大段代码或者再嵌套分 ...
- SQL中distinct的用法(转)
原文:http://www.cnblogs.com/rainman/archive/2013/05/03/3058451.html 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- jquery 上下滑动效果
<script type="text/javascript"> var myar = setInterval('AutoScroll(".li_gundong ...
- URAL 1225 Flags
题目:click here #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int n; ll ...
- iOS中解析json多种方法
我感觉JSON解析,重要的是JSON解析之后对结果的处理JSON解析后是个dictionary,但是字典中有可能包含字典和数组,数组中还可以包含字典.向客户端请求的返回数据解析下面就简单介绍一下JSO ...
- wdcp centos6.5多网站部署
一.wdcp介绍 wdCP是WDlinux Control Panel的简称,是一套通过WEB控制和管理服务器的Linux服务器管理系统以及虚拟主机管理系统,旨在易于使用Linux系统做为我们的网站服 ...
- python 字典有序无序及查找效率,hash表
刚学python的时候认为字典是无序,通过多次插入,如di = {}, 多次di['testkey']='testvalue' 这样测试来证明无序的.后来接触到了字典查找效率这个东西,查了一下,原来字 ...
- 命名空间“System.Web.Mvc”中不存在类型或命名空间“Ajax”(是否缺少程序集引用?)
原文 http://www.cnblogs.com/LJP-JumpAndFly/p/4109602.html 好吧,非常激动的说,这个问题搞了我一个晚上,网上的帖子太少了,好像不超过2篇,而且说得 ...
- The Priest Mathematician
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=31329#problem/F f[0] = 1 , f[ i ] = f[ i - 1 ] ...
- 写一方法计算实现任意个整数之和.在主调函数中调用该函数,实现任意个数之和。(使用params参数)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...