关于iOS的UIView的一点笔记

来源:互联网 发布:linux mc服务器 编辑:程序博客网 时间:2024/05/22 14:40

跟着demo做了UIView的一些相关操作后,写一些笔记。

自己添加一个VIEW在storyboard上,在右下角的object liabrary里面雪中view然后添加到view controller上,在这之前,要先创建一个自己的类,继承与UIView,用这个类来处理新添加的VIEW的操作。创建好这个类以及添加了新的VIEW之后,在右上角将这个新的VIEW的类设为新创建的类(本文中是 FaceView),然后就可以用FaceView类来处理关于这个新的视图的操作了。

这次的操作主要是有下面三部分组成,第一个是画一个笑脸,第二个是要支持左右旋转的时候,形状可以重画,第三是支持pinch(收缩)操作,两只手指控制放大缩小图形。

(一)、画笑脸

很简单的笑脸 就是一个大圆圈,里面有两个小圆圈,作为眼睛,再加一个曲线作为嘴巴。

画图的话 要注意要先获取当前的上下文

   CGContextRef context = UIGraphicsGetCurrentContext();

然后在调用函数作图的过程中

-(void)drawCircleAtPoint:(CGPoint)p withRadius:(CGFloat)radius inContext:(CGContextRef)context {    UIGraphicsPushContext(context);    CGContextBeginPath(context);    CGContextAddArc(context,p.x,p.y,radius,0,2*M_PI,YES);    CGContextStrokePath(context);    UIGraphicsPopContext();}
画一个微笑的曲线,用curve来画,要设置2个控制顶点

//draw the mouthCPoint mouthStart;mouthStart.x = midPoint.x - MOUTH_X_SCALE*size;mouthStart.y = midPoint.y + MOUTH_Y_SCALE*size;CPoint mouthEnd = mouthStart;mouthEnd.x += MOUTH_X_SCALE*size*2;//the two control pointsCPoint mouthCP1 = mouthStart;mouthCP1.x += MOUTH_X_SCALE * size * 2/3;CPoint mouthCP2 = mouthEnd;mouthCP2.x -= MOUTH_X_SCALE * size * 2/3;float smile = 1.0;CGFloat smileOffset = MOUTH_SMILE * size *smile;mouthCP1.y += smileOffset;mouthCP2.y += smileOffset;CGContextBeginPath(context);CGContextMoveToPoint(context,mouthStart.x,mouthStart.y);CGContextAddCurveToPoint(context,mouthCP1.x,mouthCP1.y,mouthCP2.x,mouthCP2.y,mouthEnd.x,mouthEnd.y);CGContextStrokePath(context);

下面就在controller中添加下面的函数,来响应旋转操作

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{return YES;}
还要添加下面的函数来重画图形,通过调用 setNeedsDisplay这个函数来实现重画图形,他会自动调用drawRect函数来重画图形,记住,永远不要自己调用drawRect来重绘

@synthesize scale = _scale;#define DEFAULT_SCALE 0.90-(CGFloat)scale{if(!_scale)return DEFAULT_SCALE;elseretuen _scale;}-(void)SetScale:(CGFloat)scale{if(_scale != scale){//redraw the view when the scale change_scale = scale;[self setNeedsDisplay];}}-(void)setup{self.contentMode = UIViewContentModeRedraw;}//关于窗口的所有信息都储存在nib文件中(“nib”是NeXT界面编辑器的缩写)。//如果执行这个方法,它会在对象被装入时从它的nib文件中被调用。-(void)awakeFromNib{[self setup];}- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {       [self setup];    }    return self;}

以上代码配合在view controller中的上一个函数的使用,就可以实现旋转的时候图形的重绘了。


最后就要来实现通过两个手指缩放对图形进行放大和缩小的操作了

Gestures are recognized by the class UIGestureRecognizer

This class is “abstract.”  We only actually use “concrete subclasses” of it.
There are two sides to using a gesture recognizer
1. Adding a gesture recognizer  to a  UIView  to ask it to recognize that gesture.
2. Providing the implementation of  a method to “handle” that gesture when it happens.

Usually #1 is done by a Controller
Though occasionally a UIView  will do it to itself if it just doesn’t make sense without that gesture.
Usually #2 is provided by the  UIView  itself
But it would not be unreasonable for the Controller to do it.
Or for the Controller to decide it wants to handle a gesture differently than the view does.

在faceview文件中就加入下面的函数

-(void)pinch:(UIPinchGestureRecognizer *)gesture{if((gesture.state == UIGestureRecognizerStateChanged) ||(gesture.state == UIGestureRecongnizerStateEnded)){self.scale *= gesture.scale;gesture.scale = 1;}}
这个是对这个动作做出的响应,也就是上面的第二步,这里是要根据gesture的scale来对原图进行缩放

下面是在view controller中的函数

-(void)setFaceView:(FaceView *)faceView{_faceView = faceView;[self.faceView addGestureRecongnizer:[[UIPinchGestureRecongnizer alloc] initithTarget:self.faceView action:@selector(pinch:)]];}

当然 还要记得在画图的函数drawRect函数中关联scale,才有效果. 图片下次再补上

原创粉丝点击