1.不能让子图层移动到父图层之外
2.如果图片为圆形,因为iphone事件是以一个矩形为事件体,只点击了圆形部分才接收事件
3.添加一个撤消管理器,在一个主要的ViewControl里面添加,其它子图层都会共享这个管理器.
ViewControl,里面添加两个button实现前进与后退
子图层
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- CGPoint pt = [[touches anyObject] locationInView:self];
- startLocation = pt;
- //将选中的图片显示在最前
- [[self superview] bringSubviewToFront:self];
- }
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- CGPoint pt = [[touches anyObject] locationInView:self];
- float dx = pt.x - startLocation.x;
- float dy = pt.y - startLocation.y;
- CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
- //计算X的边界
- float halfx = CGRectGetMidX(self.bounds);
- newcenter.x = MAX(halfx , newcenter.x);
- newcenter.x = MIN(self.superview.bounds.size.width - halfx , newcenter.x);
- //计算Y的边界
- float halfy = CGRectGetMidX(self.bounds);
- newcenter.y = MAX(halfy , newcenter.y);
- newcenter.y = MIN(self.superview.bounds.size.height - halfy , newcenter.y);
- self.center = newcenter;
- }
2.如果图片为圆形,因为iphone事件是以一个矩形为事件体,只点击了圆形部分才接收事件
- - (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
- /*
- 判断点是否在园内:
- (1)看点与圆心的距离,园方程X^2+Y^2=R^2中圆心为(0,0)半径为R.在计算所求点与圆心的距离。计算方法:若所求点坐标为(M,N)则比较M^2+N^2与R^2的大小,前大则点在园外,前小则点在园内,相等则点在园上.
- (2)基本式(X-a)^2+(Y-b)^2=r^2中园心坐标为(a,b),在判断所求点与圆心坐标的距离即可。同样若所求点坐标为(M,N),则看(M-a)^2+(N-b)^2与r^2的大小,判断方法同上.
- 最后,要判断点是否在园内,只有比较点到圆心坐标的距离就行了大于半径在园外,小于半径在园内,等于在圆上.
- */
- {
- NSLog(@"x=%f,y=%f",point.x,point.y);
- CGPoint pt;
- //点X,Y,都为32
- pt.x = (point.x - 32) / 32;
- pt.y = (point.y - 32) / 32;
- float xsquared = pt.x * pt.x;
- float ysquared = pt.y * pt.y;
- if (xsquared + ysquared <1.0f) return YES;
- return NO;
- }
3.添加一个撤消管理器,在一个主要的ViewControl里面添加,其它子图层都会共享这个管理器.
- //ViewControl,添加一个撤销管理器
- self.undoManager = [[NSUndoManager alloc] init];
- [self.undoManager setLevelsOfUndo:999];
ViewControl,里面添加两个button实现前进与后退
- - (void) undo {
- [self.undoManager undo];
- }
- - (void) redo {
- [self.undoManager redo];
- }
子图层
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- [self setPosition:self.center];
- ..........
- - (void) setPosition: (CGPoint) pos
- {
- // Prepare undo-redo first
- //注册方法
- [[self.undoManager prepareWithInvocationTarget:self] setPosition:self.center];
- // Make the change
- [UIView beginAnimations:@"" context:nil];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
- [UIView setAnimationDuration:0.1f];
- self.center = pos; // animate
- [UIView commitAnimations];
- }