国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

簡單介紹iOS下實現(xiàn)基本繪畫板功能

2020-02-19 15:54:18
字體:
供稿:網(wǎng)友

軟件繪圖會消耗相當大的內(nèi)存,但是由于項目需要還是要制作出,最近小編都在學習關于這方面的內(nèi)容,下面就跟著武林技術頻道小編的步伐來簡單介紹iOS下實現(xiàn)基本繪畫板功能吧,希望對你學習有幫助!

簡單介紹iOS下實現(xiàn)基本繪畫板功能

代碼部分
TouchView.h


#import ?
?
@interface TouchView : UIView?
{?
??? NSMutableArray *points;?
??? NSArray *points_all;?
??? CGContextRef context;?
??? UIColor *paint_clr;?
}?
@property (strong,nonatomic) NSMutableArray *points;?
@property (strong,nonatomic) NSArray *points_all;?
@property (strong,nonatomic) UIColor *paint_clr;?
?
@end?

?

TouchView.m


#import "TouchView.h"?
?
@implementation TouchView?
@synthesize points, points_all, paint_clr;?
?
- (id)initWithFrame:(CGRect)frame?
{?
??? self = [super initWithFrame:frame];?
??? if (self) {?
??????? // Initialization code?
??????? paint_clr = [UIColor greenColor];?
??? }?
??? return self;?
}?
?
// Only override drawRect: if you perform custom drawing.?
// An empty implementation adversely affects performance during animation.?
- (void)drawRect:(CGRect)rect?
{?
??? // Drawing code?
??? if ((!self.points) || (self.points.count ??????? return;?
??? }?
???????
??? context = UIGraphicsGetCurrentContext();?
??? //設置畫筆粗細??
??? CGContextSetLineWidth(context, 5.0f);?
??? //設置畫筆顏色?
??? //[[UIColor blueColor]set ];?
??? // [paint_clr set];?
??? //CGContextSetStrokeColorWithColor(context, [[UIColor blueColor]CGColor]);?
??? CGContextSetStrokeColorWithColor(context, [paint_clr CGColor]);?
?????
??? //畫以前的軌跡?
??? for (int j = 0 ; j ??????? NSMutableArray *points_tmp = [points_all objectAtIndex:j];?
?????????????
??????????? for (int i = 0;i ??????????? {?
??????????????? CGPoint point1 = [[points_tmp objectAtIndex:i] CGPointValue];?
??????????????? CGPoint point2 = [[points_tmp objectAtIndex:(i+1)] CGPointValue];?
??????????????? CGContextMoveToPoint(context, point1.x, point1.y);?
??????????????? CGContextAddLineToPoint(context, point2.x, point2.y);?
??????????????? CGContextStrokePath(context);?
??????????? }?
??????? }?
?????
??? //畫這次?
??? for (int i=0; i ??????? CGPoint point1 = [[self.points objectAtIndex:i] CGPointValue];?
??????? CGPoint point2 = [[self.points objectAtIndex:(i+1)] CGPointValue];?
??????? CGContextMoveToPoint(context, point1.x, point1.y);?
??????? CGContextAddLineToPoint(context, point2.x, point2.y);?
??????? CGContextStrokePath(context);?
??? }?????
}?
?
//不支持多點觸摸?
- (BOOL) isMultipleTouchEnabled?
{?
??? return NO;?
}?
?
//創(chuàng)建一個array,并且記錄初始ponit?
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event?
{?
??? self.points = [NSMutableArray array];?
??? CGPoint pt = [[touches anyObject] locationInView:self];?
??? [self.points addObject:[NSValue valueWithCGPoint:pt]];?
}?
?
//移動過程中記錄這些points?
//調(diào)用setNeedsDisplay,會觸發(fā)drawRect方法的調(diào)用?
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event?
{?
??? CGPoint pt = [[touches anyObject] locationInView:self];?
??? [self.points addObject:[NSValue valueWithCGPoint:pt]];?
??? [self setNeedsDisplay];?
}?
?
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event?
{?
??? NSMutableArray *points_tmp = [[NSMutableArray alloc] initWithArray:self.points];?
??? if (self.points_all == nil) {?
??????? self.points_all = [[NSArray alloc] initWithObjects:points_tmp, nil];?
??? }else {?
??????? self.points_all = [self.points_all arrayByAddingObject:points_tmp];?
??? }?
}?
@end?

?

ViewController.h


#import ?
?
@class TouchView;?
@interface ViewController : UIViewController?
{?
??? TouchView *tv;?
}?
@end?

?

ViewController.m


#import "ViewController.h"?
#import "TouchView.h"?
?
@interface ViewController ()?
?
@end?
?
@implementation ViewController?
?
- (void)viewDidLoad?
{?
??? [super viewDidLoad];?
??? // Do any additional setup after loading the view, typically from a nib.?
??? self.view.userInteractionEnabled = YES;?
?????
? // TouchView *tv = [[TouchView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 260.0f, 260.0f)];?
??? tv = [[TouchView alloc]initWithFrame:self.view.frame];?
??? tv.backgroundColor = [UIColor blackColor];?
?????
??? [self.view addSubview:tv];?
?????
??? UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[@"White Red Blue Green Yellow" componentsSeparatedByString:@" "]];?
??? seg.segmentedControlStyle = UISegmentedControlSegmentCenter;?
??? seg.tintColor = [UIColor blackColor];??
??? seg.center = CGPointMake(self.view.center.x, (self.view.bounds.size.height - seg.bounds.size.height));??
??? [self.view addSubview:seg];?
?????
??? [seg addTarget:self action:@selector(colorChange:) forControlEvents:UIControlEventValueChanged];?
}?
?
- (void)viewDidUnload?
{?
??? [super viewDidUnload];?
??? // Release any retained subviews of the main view.?
}?
?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?
{?
??? return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);?
}?
?
- (void) colorChange: (UISegmentedControl *) seg?
{?
??? switch ([seg selectedSegmentIndex])?
??? {?
??????? case 0:??
??????????? tv.paint_clr = [UIColor whiteColor];?
??????????? break;?
??????? case 1:?
??????????? tv.paint_clr = [UIColor redColor];?
??????????? break;?
??????? case 2:?
??????????? tv.paint_clr = [UIColor blueColor];?
??????????? break;?
??????? case 3:?
??????????? tv.paint_clr = [UIColor greenColor];?
??????????? break;?
??????? case 4:?
??????????? tv.paint_clr = [UIColor yellowColor];?
??????????? break;?
??????? default:?
?????????????
??????????? break;?
??? }?
}?
?
@end?

?

效果圖

20151030100650479.png (320×480)

上文是武林技術頻道小編簡單介紹iOS下實現(xiàn)基本繪畫板功能,感興趣的朋友們可以繼續(xù)關注武林技術頻道哦!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 日照市| 岑溪市| 龙南县| 盱眙县| 乌兰浩特市| 通许县| 嘉鱼县| 确山县| 习水县| 神木县| 田阳县| 南木林县| 凌海市| 庆安县| 赣榆县| 淮安市| 澄迈县| 建平县| 桑植县| 广灵县| 昭觉县| 灵寿县| 淮南市| 泸溪县| 晋宁县| 民丰县| 如皋市| 三门峡市| 安福县| 西乌| 贵南县| 普安县| 武城县| 宣武区| 石棉县| 泽库县| 曲靖市| 上饶县| 梨树县| 孟连| 大石桥市|