热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

(7/18)重学Standford_iOS7开发_视图、绘制、手势识别_课程笔记

UIBezierPath*roundedRect=[UIBezierPathbezierPathWithRoundedRect:(CGRect)boundscornerRadius:(

UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:(CGRect)bounds cornerRadius:(CGFloat)radius];//绘制圆角矩形
//绘制椭圆
UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:(CGRect)bounds];
//剪裁视图
[roundedRect addClip];//剪裁后的视图只能在其路径区域内绘制,超出部分不会绘制

c.透明度相关

*UIColor:属性alpha(0.0-1.0)

*UIView:(BOOL)opaque(不透明),alpha(0.0-1.0),hidden(隐藏视图)

区别请看:

d.子视图与父视图转换时上下文内容变化的问题

压入(push),取出(pop)状态

- (void)drawGreenCircle:(CGContextRef)ctxt
{
CGContextSaveGState(ctxt);//保存当前上下文
[[UIColor greenColor] setFill];
// draw my circle
CGContextRestoreGState(ctxt);//恢复保存的上下文
}
- (void)drawRect:(CGRect)aRect
{
CGContextRef cOntext= UIGraphicsGetCurrentContext();
[[UIColor redColor] setFill];
// do some stuff
[self drawGreenCircle:context];
// do more stuff and expect fill color to be red
}

e.绘制文本

使用NSAttributeString

NSAttributedString *text = ...;//创建绘制内容
CGSize textSize = [text size];//获取文本尺寸大小
[text drawAtPoint:(CGPoint)p];//将文本绘制到指定位置(左上角),或者使用drawInRect也可以

f.绘制图片

UIImage *image = [UIImage imageNamed:@ foo.jpg ];
//UIImage *image = [[UIImage alloc] initWithContentsOfFile:(NSString *)fullPath];
//UIImage *image = [[UIImage alloc] initWithData:(NSData *)imageData];
//使用上下文绘制
UIGraphicsBeginImageContext(CGSize);
// draw with CGContext functions
UIImage *myImage = UIGraphicsGetImageFromCurrentContext();
UIGraphicsEndImageContext();
//标准绘制
[image drawAtPoint:(CGPoint)p];
//[image drawInRect:(CGRect)r];
//[image drawAsPatternInRect:(CGRect)patRect;

g.bounds变化时视图的重绘

UIView属性:@property (nonatomic) UIViewContentMode contentMode;

//位置重绘
UIViewContentMode{Left,Right,Top,Right,BottomLeft,BottomRight,TopLeft,TopRight}
//缩放重绘
UIViewContentModeScale{ToFill,AspectFill,AspectFit} // bit stretching/shrinking
//bounds变化时调用drawRect重绘
UIViewContentModeRedraw // it is quite often that this is what you want

2、手势识别

步骤:a.创建手势识别器,添加到视图

b.实现手势触发时的调用方法

①UIGestureRecognizer

抽象超类,所有具体手势类的父类

②添加手势控制

- (void)setPannableView:(UIView *)pannableView // maybe this is a setter in a Controller
{
_pannableView = pannableView;
UIPanGestureRecognizer *pangr =
[[UIPanGestureRecognizer alloc] initWithTarget:pannableView action:@selector(pan:)];//target也可是视图控制器,pan为触发时的调用方法,由target类实现
[pannableView addGestureRecognizer:pangr];//讲手势添加到视图
}

③pan手势的例子

- (CGPoint)translationInView:(UIView *)aView;//触摸移动的距离
- (CGPoint)velocityInView:(UIView *)aView;//移动速度
- (void)setTranslation:(CGPoint)translation inView:(UIView *)aView;

④抽象超类提供的state属性

//UIGestureRecognizerStateBegin 连续手势开始
//UIGestureRecognizerStateChanged 移动
//UIGestureRecognizerStateEnded
//UIGestureRecognizerStateCancelled
//UIGestureRecognizerStateFailed
//UIGestureRecognizerStateRecognized 识别到手势
//使用举例
- (void)pan:(UIPanGestureRecognizer *)recognizer
{
if ((recognizer.state == UIGestureRecognizerStateChanged) ||
(recognizer.state == UIGestureRecognizerStateEnded))
{
CGPoint translation = [recognizer translationInView:self];
// move something in myself (I m a UIView) by translation.x and translation.y
// for example, if I were a graph and my origin was set by an @property called
origin self.origin = CGPointMake(self.origin.x+translation.x, self.origin.y+translation.y);
[recognizer setTranslation:CGPointZero inView:self];//恢复手势移动距离,为下次手势识别调用初始化
 }
}

⑤其他手势属性

//UIPinchGestureRecognizer 捏合手势
@property CGFloat scale; // 缩放比例
@property (readonly) CGFloat velocity; //速度(readonly)
UIRotationGestureRecognizer 旋转手势
@property CGFloat rotation; // 旋转弧度
@property (readonly) CGFloat velocity; //速度(readonly)
UISwipeGestureRecognizer 滑动手势
@property UISwipeGestureRecognizerDirection direction; //方向(4)
@property NSUInteger numberOfTouchesRequired; // 触控数量
UITapGestureRecognizer 点击手势
@property NSUInteger numberOfTapsRequired; // 点击次数
@property NSUInteger numberOfTouchesRequired; //触控数量

3、其他

#pragma mark - example

编译器标记,对方法进行分组,结果如下

5、demo

SuperCard:

课程视频地址:网易公开课:

或者iTunes U搜索standford课程


推荐阅读
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有