手势与触摸
如果要VIEW支持用户交互,得要设置
?
self.userInteractionEnabled = YES;
?一个简单的UIView例子
?
@interface DragView : UIImageView{CGPoint startLocation;}@end@implementation DragView- (id) initWithImage: (UIImage *) anImage{if (self = [super initWithImage:anImage])self.userInteractionEnabled = YES;return self;}- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{// Calculate and store offset, and pop view into front if neededCGPoint pt = [[touches anyObject] locationInView:self];startLocation = pt;[[self superview] bringSubviewToFront:self];NSLog(@"begin");}- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{// Calculate offsetCGPoint 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);// Bound movement into parent boundsfloat halfx = CGRectGetMidX(self.bounds);newcenter.x = MAX(halfx, newcenter.x);newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);float halfy = CGRectGetMidY(self.bounds);newcenter.y = MAX(halfy, newcenter.y);newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);// Set new locationself.center = newcenter;}@end?手动创建一图片的UIImageView
?
- (UIImage *) createImage{UIColor *color = [UIColor colorWithRed:RANDLEVEL green:RANDLEVEL blue:RANDLEVEL alpha:1.0f]; //设置图片的区域UIGraphicsBeginImageContext(CGSizeMake(SIDELENGTH, SIDELENGTH));CGContextRef context = UIGraphicsGetCurrentContext();// Create a filled ellipse 填充区域[color setFill];CGRect rect = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH);CGContextAddEllipseInRect(context, rect);CGContextFillPath(context);// Outline the circle a couple of timesCGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] CGColor]);CGContextAddEllipseInRect(context, CGRectInset(rect, INSET_AMT, INSET_AMT));CGContextStrokePath(context);CGContextAddEllipseInRect(context, CGRectInset(rect, 2*INSET_AMT, 2*INSET_AMT));CGContextStrokePath(context);UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return theImage;}?判断是否在区域里面?HALFSIDE是圆的半径
?
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event {CGPoint pt;float HALFSIDE = SIDELENGTH / 2.0f;// normalize with centered originpt.x = (point.x - HALFSIDE) / HALFSIDE;pt.y = (point.y - HALFSIDE) / HALFSIDE;// x^2 + y^2 = radiusfloat xsquared = pt.x * pt.x;float ysquared = pt.y * pt.y;// If the radius < 1, the point is within the clipped circleif ((xsquared + ysquared) < 1.0) return YES;return NO;}根据图像的像位判断是否点击到图片,位图上面的触摸
?
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event { //如果点下去的点不在self.bounds里面直接返回NOif (!CGRectContainsPoint(self.bounds, point)) {return NO;} //如果这个点的中断值 ALPHA小于33%,就可以看做是透明了,但也要和实际情况return (bytes[alphaOffset(point.x, point.y, self.image.size.width)] > 85);}//计算当前点在数像中的位的位置//X,Y表示点的坐标,W表示当前VIEW的宽度NSUInteger alphaOffset(NSUInteger x, NSUInteger y, NSUInteger w){return y * w * 4 + x * 4 + 0;}//计算图像的位unsigned char *getBitmapFromImage (UIImage *image){ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();if (colorSpace == NULL) { fprintf(stderr, "Error allocating color space\n"); return NULL; }CGSize size = image.size;// void *bitmapData = malloc(size.width * size.height * 4);unsigned char *bitmapData = calloc(size.width * size.height * 4, 1); // Courtesy of Dirk. Thanks! if (bitmapData == NULL) { fprintf (stderr, "Error: Memory not allocated!"); CGColorSpaceRelease(colorSpace); return NULL; } CGContextRef context = CGBitmapContextCreate (bitmapData, size.width, size.height, 8, size.width * 4, colorSpace, kCGImageAlphaPremultipliedFirst); CGColorSpaceRelease(colorSpace ); if (context == NULL) { fprintf (stderr, "Error: Context not created!"); free (bitmapData);return NULL; }CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);CGContextDrawImage(context, rect, image.CGImage);unsigned char *data = CGBitmapContextGetData(context);CGContextRelease(context); return data;}视图持久性和归档
?
//持久化- (void) updateDefaults{NSMutableArray *colors = [[NSMutableArray alloc] init];NSMutableArray *locs = [[NSMutableArray alloc] init];for (DragView *dv in [[self.view viewWithTag:201] subviews]) {[colors addObject:dv.whichFlower];[locs addObject:NSStringFromCGRect(dv.frame)];}[[NSUserDefaults standardUserDefaults] setObject:colors forKey:@"colors"];[[NSUserDefaults standardUserDefaults] setObject:locs forKey:@"locs"];[[NSUserDefaults standardUserDefaults] synchronize];[colors release];[locs release];}//删除[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"colors"];[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"locs"];//加载NSMutableArray *colors = [[NSUserDefaults standardUserDefaults] objectForKey:@"colors"];NSMutableArray *locs = [[NSUserDefaults standardUserDefaults] objectForKey:@"locs"];//归档@interface DragView : UIImageView{CGPoint startLocation;NSString *whichFlower;}@property (retain) NSString *whichFlower;@end@implementation DragView@synthesize whichFlower;//保存- (void) encodeWithCoder: (NSCoder *)coder{[coder encodeCGRect:self.frame forKey:@"viewFrame"];[coder encodeObject:self.whichFlower forKey:@"flowerType"];}//初始化- (id) initWithCoder: (NSCoder *)coder{[super initWithFrame:CGRectZero];self.frame = [coder decodeCGRectForKey:@"viewFrame"];self.whichFlower = [coder decodeObjectForKey:@"flowerType"];self.image = [UIImage imageNamed:self.whichFlower];self.userInteractionEnabled = YES;return self;}//保存NSArray *flowers = [[self.view viewWithTag:201] subviews];[NSKeyedArchiver archiveRootObject:flowers toFile:DATAPATH];//加载NSArray *flowers = [NSKeyedUnarchiver unarchiveObjectWithFile:DATAPATH];? 使用NSUndoManager来执行Undo和reDo操作
// Initialize the undo manager for this applicationself.undoManager = [[NSUndoManager alloc] init];//设置UNDO次数[self.undoManager setLevelsOfUndo:999];//注册 在视图时同注册[[self.undoManager prepareWithInvocationTarget:self] setPosition:self.center];//然后是否正在undowhile ([self.undoManager isUndoing]);// Don't show the undo button if the undo stack is empty//是否能undoif (!self.undoManager.canUndo) self.navigationItem.leftBarButtonItem = nil;elseself.navigationItem.leftBarButtonItem = BARBUTTON(@"Undo", @selector(undo));//最后再处理undo[self.undoManager undo];??
?
?
?
?
?
?
预留
预留
预留
预留
预留