ÏÂÃæµÄ¶«Î÷ÊDZàд×Ô¶¨ÒåµÄ±íÇé¼üÅÌ£¬»°²»¶à˵£¬¿ªÃżûɽ°É£¡ÏÂÃæÖ÷ÒªÓõ½µÄ֪ʶÓÐMVC,
iOS¿ª·¢ÖеÄ×Ô¶¯²¼¾Ö£¬×Ô¶¨Òå×é¼þµÄ·â×°ÓëʹÓã¬Block»Øµ÷£¬CoreDataµÄʹÓá£ÓеÄС»ï°é¿ÉÄÜ»áÎÊдһ¸ö×Ô¶¨Òå±íÇé¼üÅÌÖ×ôÕâôÂé·³£¿ÏÂÃæ½«»á½éÉÜÎÒÃÇÈçºÎÓÃÉÏÃæÌáµ½µÄ¶«Î÷À´¶¨ÒåÎÒÃǵıíÇé¼üÅÌ¡£ÏÂÃæµÄÄÚÈÝ»á±È½Ï¶à£¬ÕâÆª²©ÎÄ»¹ÊDZȽÏÓÐÁϵġ£
»¹ÊÇÄǾ仰д¼¼Êõ²©¿ÍÊÇÉÙ²»ÁË´úÂëµÄ£¬ÏÂÃæ»á½áºÏ´úÂëÀ´»Ø¹ËÒ»ÏÂiOSµÄ֪ʶ£¬±¾Æª²©ÎÄÖÐÓõ½µÄ֪ʶµãÔÚÇ°ÃæµÄ²©¿ÍÖж¼ÄÜÕÒµ½ÏàÓ¦µÄÄÚÈÝ£¬±¾ÆªËãÊÇÒ»¸öССµÄ¹¦ÄÜÕûºÏ¡£ÏÈÀ´ÕÅͼ¿´Ò»Ï±¾appµÄĿ¼½á¹¹¡£ÎÒÊǸù¾Ý×Ô¼º¶ÔMVCµÄÀí½âÀ´¹¹½¨µÄĿ¼½á¹¹£¬Ï£ÍûÆðµ½Å×שÒýÓñµÄ×÷Óã¬ÓкõĽâ¾ö·½°¸»¶ÓÆÀÂÛ»òÕßÁôÑÔÖ¸³ö¡£FaceÎļþÖдæ·ÅµÄʱÎÒÃǵıíÇéͼƬ£¬ModelÎļþ·â×°µÄÊÇ´ÓsqliteÖжÁÈ¡ÀúÊ·Í·ÏñµÄ×é¼þ£¬ViewÎļþÖзâ×°µÄʱÎÒÃÇ×Ô¶¨ÒåµÄ×é¼þ£¬Ò²¾ÍÊÇ×Ô¶¨Òå¼üÅÌÏà¹ØµÄÊÓͼ£¬Controller¸ºÔð½«ÎÒÃǵĸ÷¸ö×é¼þ×é×°µ½Ò»ÆðÍê³ÉÎÒÃÇÏëÒªµÄ¹¦ÄÜ¡£ÏÂÃæ»áÒ»Ò»½éÉÜ¡£

ÉÏÃæÊÇÎļþµÄ×éÖ¯½á¹¹£¬ÏÂÃæÎªÁ˸üΪֱ¹ÛµÄÁ˽âÎÒÃÇÏëÒªµÄЧ¹û£¬ÏÂÃæÏÈ¿´¼¸ÕŽØÍ¼£¬À´Ö±¹ÛµÄ¸ÐÊÜÒ»ÏÂÔËÐÐЧ¹û£¬ÉÏÃæÊÇÊúÆÁµÄÏÔʾЧ¹û£¬ÏÂÃæÊǺáÆÁµÄÏÔʾЧ¹û¡£ÒòΪÔÚ·â×°×Ô¶¨Òå¼üÅÌÖÐÓõ½ÁË×Ô¶¯²¼¾ÖËùÒÔºáÆÁÏÔʾ»òÕßÔÚ¸ü´óµÄÆÁÄ»ÉÏÏÔʾÊÇûÎÊÌâµÄ£¬³£ÓñíÇéÊÇÓû§ÓùýµÄ±íÇ飬Ȼºó´æÔÚSqliteÖУ¬ÏÔʾʱ²¢°´Ê±¼ä½µÐòÅÅÁС£moreÊÇÓÃÀ´À©Õ¹¹¦ÄÜÓõĽӿڡ£»°²»¶à˵£¬À´µÄ´úÂë²ÅÊÇʵÔڵġ£


Ò».View(×Ô¶¨ÒåÊÓͼ)
ViewÎļþ¼ÐÏ´æ·ÅµÄʱÎÒÃÇ×Ô¶¨ÒåµÄÊÓͼ×é¼þ£¬ÒòΪÊÇ×Ô¶¨ÒåµÄ×é¼þËùÒÔstoryboardÎÒÃǾÍÓò»ÁËÀ²£¬ËùÓеĴúÂë¶¼±ØÐëÊÖд£¬ÕâÑù²ÅÄܱ£Ö¤×é¼þʹÓõÄÁé»îÐԺͼõÉÙ¸÷¸ö×é¼þÖ®¼äµÄñîºÏÐÔ£¬¸üÀûÓÚÍŶÓÖ®¼äµÄºÏ×÷¡£ÔÚ·â×°×é¼þʱҪԤÁôºÃÍâ½ç¿ÉÄÜʹÓõ½µÄ½Ó¿Ú£¬ºÍ·µ»Ø¸Ã·µ»ØµÄÊý¾Ý¡£ºÃÀ²£¬·Ï»°ÉÙ˵£¬À´µã¸É»õ°É£¡
1¡¢FaceView×é¼þµÄ·â×°£ºFaceView¼´¸ºÔðÏÔʾһ¸ö¸öµÄÍ·Ïñ¡£ÔÚʹÓøÃ×é¼þʱҪ´«ÈëÒªÏÔʾµÄͼƬºÍͼƬ¶ÔÓ¦µÄÎÄ×Ö£¨È硾¹þ¹þ¡¿£©£¬µ±µã»÷ͼƬµÄʱºò£¬»áͨ¹ýblock»Øµ÷µÄÐÎʽ°Ñ¸ÃͼƬµÄimageÒÔ¼°Í¼Æ¬ÎÄ×Ö·µ»Øµ½Ê¹ÓõÄ×é¼þÖÐÈ¥£¬ÏÂÃæÊǹؼü´úÂ룺
FaceView.hÖеĴúÂëÈçÏ£¨ÏÂÃæ´úÂëÊǶ¨ÒåÀ²ÏàÓ¦µÄBlockÀàÐͺͶÔÍâµÄ½Ó¿Ú£©£º
#import
//ÉùÃ÷±íÇé¶ÔÓ¦µÄblock,ÓÃÓڰѵã»÷µÄ±íÇéµÄͼƬºÍͼƬÐÅÏ¢´«µ½ÉϲãÊÓͼ
typedef void (^FaceBlock) (UIImage *image, NSString *imageText);
@interface FaceView : UIView
//ͼƬ¶ÔÓ¦µÄÎÄ×Ö
@property (nonatomic, strong) NSString *imageText;
//±íÇéͼƬ
@property (nonatomic, strong) UIImage *headerImage;
//ÉèÖÃblock»Øµ÷
-(void)setFaceBlock:(FaceBlock)block;
//ÉèÖÃͼƬ£¬ÎÄ×Ö
-(void)setImage:(UIImage *) image ImageText:(NSString *) text;
@end
|
FaceView.mÖеĴúÂëÈçÏ£º
//
// FaceView.m
// MyKeyBoard
//
// Created by ÇàÓñ·ü°¸ on 14-9-16.
// Copyright (c) 2014Äê Mrli. All rights reserved.
//
#import "FaceView.h"
@interface FaceView ()
@property(strong, nonatomic) FaceBlock block;
@property (strong, nonatomic) UIImageView *imageView;
@end
@implementation FaceView
//³õʼ»¯Í¼Æ¬
- (id)initWithFrame:(CGRect)frame
{
//faceµÄ´óС
frame.size.height = 30;
frame.size.width = 30;
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[self addSubview:self.imageView];
}
return self;
}
-(void) setFaceBlock:(FaceBlock)block
{
self.block = block;
}
-(void) setImage:(UIImage *)image ImageText:(NSString *)text
{
//ÏÔʾͼƬ
[self.imageView setImage:image];
//°ÑͼƬ´æ´¢ÆðÀ´
self.headerImage = image;
self.imageText = text;
}
//µã»÷ʱ»Øµ÷
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
//Åжϴ¥ÃþµÄ½áÊøµãÊÇ·ñÔÚͼƬÖÐ
if (CGRectContainsPoint(self.bounds, point))
{
//»Øµ÷,°Ñ¸ÃÍ·ÏñµÄÐÅÏ¢´«µ½ÏàÓ¦µÄcontrollerÖÐ
self.block(self.headerImage, self.imageText);
}
}
@end
|
´úÂë˵Ã÷£º
Ö÷Òª¾ÍÊÇblock»Øµ÷µÄʹÓ㬾ÍÊÇ·â×°ÁËÒ»¸ö×Ô¶¨ÒåµÄbutton,¾ßÌåÄÚÈÝÇë²Î¿¼Ö®Ç°µÄ²©¿Í¡°IOS¿ª·¢Ö®×Ô¶¨ÒåButton(¼¯³ÉÈýÖֻص÷ģʽ)¡±
2¡¢FunctionView×é¼þµÄ·â×°£¬FunctionView¾ÍÊÇʹÓÃFaceView×é¼þºÍScrollView×é¼þ°Ñ±íÇé¼ÓÔØ½øÀ´£¬ÔÚʵÀý»¯FunctionView×é¼þʱ£¬ÎÒÃÇÓõ½ÁË×Ô¶¯²¼¾ÖÀ´ÉèÖÃScrollViewºÍÏÂÃæµÄButton
FunctionView.hµÄ´úÂëÈçÏÂ,ÔÚ.hÖÐÁôÓÐ×é¼þµÄ½Ó¿ÚºÍ»Øµ÷ÓõÄBlock,
plistFileNameÓÃÓÚ¼ÓÔØÎÒÃǵÄ×ÊÔ´ÎļþʱʹÓã¬ÖÁÓÚÈçºÎʹÓÃplistÎļþ£¬Çë²Î¿¼Ö®Ç°µÄ²©¿Í£ºIOS¿ª·¢Ö®ÏÔʾ΢²©±íÇé¡£
//
// FunctionView.h
// MyKeyBoard
//
// Created by ÇàÓñ·ü°¸ on 14-9-16.
// Copyright (c) 2014Äê Mrli. All rights reserved.
//
#import
//¶¨Òå¶ÔÓ¦µÄblockÀàÐÍ£¬ÓÃÓÚÊý¾ÝµÄ½»»¥
typedef void (^FunctionBlock) (UIImage *image, NSString *imageText);
@interface FunctionView : UIView
//×ÊÔ´ÎļþÃû
@property (nonatomic, strong) NSString *plistFileName;
//½ÓÊÜblock¿é
-(void)setFunctionBlock:(FunctionBlock) block;
@end
|
FunctionView.mÖеĴúÂëÈçÏ£¬³£ÓñíÇéÊÇÔÚsqliteÖлñÈ¡µÄ£¬¶øÈ«²¿±íÇéÊÇͨ¹ýplistÎļþµÄÐÅÏ¢ÔÚFaceÎļþÖмÓÔØµÄ£º
//
// FunctionView.m
// MyKeyBoard
//
// Created by ÇàÓñ·ü°¸ on 14-9-16.
// Copyright (c) 2014Äê Mrli. All rights reserved.
//
#import "FunctionView.h"
#import "FaceView.h"
#import "ImageModelClass.h"
#import "HistoryImage.h"
@interface FunctionView()
@property (strong, nonatomic) FunctionBlock block;
//ÔÝ´æ±íÇé×é¼þ»Øµ÷µÄ±íÇéºÍ±íÇéÎÄ×Ö
@property (strong, nonatomic) UIImage *headerImage;
@property (strong, nonatomic) NSString *imageText;
//displayÎÒÃǵıíÇéͼƬ
@property (strong, nonatomic) UIScrollView *headerScrollView;
//¶¨ÒåÊý¾ÝÄ£ÐÍÓÃÓÚ»ñÈ¡ÀúÊ·±íÇé
@property (strong, nonatomic) ImageModelClass *imageModel;
@end
@implementation FunctionView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//ʵÀý»¯Êý¾ÝÄ£ÐÍ
self.imageModel =[[ImageModelClass alloc] init];
//ʵÀý»¯ÏÂÃæµÄbutton
UIButton *faceButton = [[UIButton alloc] initWithFrame:CGRectZero];
faceButton.backgroundColor = [UIColor grayColor];
[faceButton setTitle:@"È«²¿±íÇé" forState:UIControlStateNormal];
[faceButton setShowsTouchWhenHighlighted:YES];
[faceButton addTarget:self action:@selector(tapButton1:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:faceButton];
//ʵÀý»¯³£ÓñíÇé°´Å¥
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectZero];
moreButton.backgroundColor = [UIColor orangeColor];
[moreButton setTitle:@"³£ÓñíÇé" forState:UIControlStateNormal];
[moreButton setShowsTouchWhenHighlighted:YES];
[moreButton addTarget:self action:@selector(tapButton2:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:moreButton];
//¸ø°´Å¥Ìí¼ÓÔ¼Êø
faceButton.translatesAutoresizingMaskIntoConstraints = NO;
moreButton.translatesAutoresizingMaskIntoConstraints = NO;
//Ë®Æ½Ô¼Êø
NSArray *buttonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:
|[faceButton][moreButton(==faceButton)]|"
options:0 metrics:0 views:NSDictionaryOfVariableBindings(faceButton,moreButton)];
[self addConstraints:buttonH];
//´¹Ö±Ô¼Êø
NSArray *button1V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:
[faceButton(44)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(faceButton)];
[self addConstraints:button1V];
NSArray *button2V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:
[moreButton(44)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(moreButton)];
[self addConstraints:button2V];
//ĬÈÏÏÔʾ±íÇéͼƬ
[self tapButton1:nil];
}
return self;
}
//½ÓÊܻص÷
-(void)setFunctionBlock:(FunctionBlock)block
{
self.block = block;
}
//µã»÷È«²¿±íÇé°´Å¥»Øµ÷·½·¨
-(void)tapButton1: (id) sender
{
// ´ÓplistÎļþÔØÈë×ÊÔ´
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:self.plistFileName ofType:@"plist"];
NSArray *headers = [NSArray arrayWithContentsOfFile:path];
if (headers.count == 0) {
NSLog(@"·ÃÎʵÄplistÎļþ²»´æÔÚ");
}
else
{
//µ÷ÓÃheaders·½·¨ÏÔʾ±íÇé
[self header:headers];
}
}
//µã»÷ÀúÊ·±íÇéµÄ»Øµ÷·½·¨
-(void) tapButton2: (id) sender
{
//´ÓÊý¾Ý¿âÖвéѯËùÓеÄͼƬ
NSArray *imageData = [self.imageModel queryAll];
//½âÎöÇëÇóµ½µÄÊý¾Ý
NSMutableArray *headers = [NSMutableArray arrayWithCapacity:imageData.count];
//Êý¾ÝʵÌ壬Ï൱ÓÚjavaBeanµÄ¶«Î÷
HistoryImage *tempData;
for (int i = 0; i < imageData.count; i ++) {
tempData = imageData[i];
//½âÎöÊý¾Ý£¬×ª»»³Éº¯ÊýheadersÒªÓõÄÊý¾Ý¸ñʽ
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:2];
[dic setObject:tempData.imageText forKey:@"chs"];
UIImage *image = [UIImage imageWithData:tempData.headerImage];
[dic setObject:image forKey:@"png"];
[headers addObject:dic];
}
[self header:headers];
}
//¸ºÔð°Ñ²é³öÀ´µÄͼƬÏÔʾ
-(void) header:(NSArray *)headers
{
[self.headerScrollView removeFromSuperview];
self.headerScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.headerScrollView];
//¸øscrollViewÌí¼ÓÔ¼Êø
self.headerScrollView.translatesAutoresizingMaskIntoConstraints = NO;
//Ë®Æ½Ô¼Êø
NSArray *scrollH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|
-10-[_headerScrollView]-10-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_headerScrollView)];
[self addConstraints:scrollH];
//´¹Ö±Ô¼Êø
NSArray *scrolV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|
-10-[_headerScrollView]-50-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_headerScrollView)];
[self addConstraints:scrolV];
CGFloat scrollHeight = (self.frame).size.height-60;
//¸ù¾ÝͼƬÁ¿À´¼ÆËãscrollViewµÄContainµÄ¿í¶È
CGFloat width = (headers.count/(scrollHeight/30))*30;
self.headerScrollView.contentSize = CGSizeMake(width, scrollHeight);
self.headerScrollView.pagingEnabled = YES;
//Í¼Æ¬×ø±ê
CGFloat x = 0;
CGFloat y = 0;
//ÍùscrollÉÏÌùͼƬ
for (int i = 0; i < headers.count; i ++) {
//»ñȡͼƬÐÅÏ¢
UIImage *image;
if ([headers[i][@"png"] isKindOfClass:[NSString class]])
{
image = [UIImage imageNamed:headers[i][@"png"]];
}
else
{
image = headers[i][@"png"];
}
NSString *imageText = headers[i][@"chs"];
//¼ÆËãͼƬλÖÃ
y = (i%(int)(scrollHeight/30)) * 30;
x = (i/(int)(scrollHeight/30)) * 30;
FaceView *face = [[FaceView alloc] initWithFrame:CGRectMake(x, y, 0, 0)];
[face setImage:image ImageText:imageText];
//faceµÄ»Øµ÷£¬µ±faceµã»÷ʱ»ñÈ¡faceµÄͼƬ
__weak __block FunctionView *copy_self = self;
[face setFaceBlock:^(UIImage *image, NSString *imageText)
{
copy_self.block(image, imageText);
}];
[self.headerScrollView addSubview:face];
}
[self.headerScrollView setNeedsDisplay];
}
@end
|
´úÂë˵Ã÷£º
1¡¢Ö÷ÒªÊÇͨ¹ý¶Ô×ÊÔ´Îļþ»òÕß¶Ô´ÓÊý¾Ý¿âÖвéѯµÄ×ÊÔ´½øÐбéÀúÈ»ºóÌí¼Óµ½ScrollViewÖÐ
2.ΪÁËÊÊÓ¦²»Í¬µÄÆÁÄ»¸øÏàÓ¦µÄ×é¼þÌí¼ÓÁËÔ¼Êø
3.ToolView×é¼þµÄ·â×°£º ToolView¾ÍÊÇÔÚÖ÷ÆÁÄ»ÉÏÏÂÃæµÄÀàËÆÓÚTabBarµÄ¶«Î÷£¬µ±¼üÅ̳öÀ´µÄʱºò,ToolView»áÔ˶¯µ½¼üÅÌÉÏÃæµÄλÖá£ÎªÁËʹÓò»Í¬µÄÆÁÄ»£¬Ò²ÐèÒªÓÃ×Ô¶¯²¼¾ÖÀ´ÊµÏÖ¡£
ToolView.hµÄ´úÂëÈçÏ£ºÔ¤Áô×é¼þ½Ó¿ÚºÍÉùÃ÷blockÀàÐÍ
//
// ToolView.h
// MyKeyBoard
//
// Created by ÇàÓñ·ü°¸ on 14-9-16.
// Copyright (c) 2014Äê Mrli. All rights reserved.
//
/*****************
·â×°ÏÂÃæµÄ¹¤¾ßÌõ×é¼þ
*****************/
#import
//¶¨Òåblock¿é±äÁ¿ÀàÐÍ£¬ÓÃÓڻص÷,°Ñ±¾ViewÉϵİ´Å¥µÄindex´«µ½ControllerÖÐ
typedef void (^ToolIndex) (NSInteger index);
@interface ToolView : UIView
//¿é±äÁ¿ÀàÐ͵Äsetter·½·¨
-(void)setToolIndex:(ToolIndex) toolBlock;
@end
|
ToolView.mµÄ´úÂëʵÏÖ£º
//
// ToolView.m
// MyKeyBoard
//
// Created by ÇàÓñ·ü°¸ on 14-9-16.
// Copyright (c) 2014Äê Mrli. All rights reserved.
//
#import "ToolView.h"
@interface ToolView ()
//¶¨ÒåToolIndexÀàÐ͵Äblock,ÓÃÓÚ½ÓÊÜÍâ½ç´«¹ýÀ´µÄblock
@property (nonatomic, strong) ToolIndex myBlock;
@end
@implementation ToolView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//1³õʼ»¯±íÇé°´Å¥
UIButton *faceButton = [[UIButton alloc] initWithFrame:CGRectZero];
faceButton.backgroundColor = [UIColor orangeColor];
[faceButton setTitle:@"񡀂" forState:UIControlStateNormal];
[faceButton setShowsTouchWhenHighlighted:YES];
[faceButton addTarget:self action:@selector(tapFaceButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:faceButton];
//³õʼ»¯¸ü¶à°´Å¥
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectZero];
moreButton.backgroundColor = [UIColor grayColor];
[moreButton setTitle:@"More" forState:UIControlStateNormal];
[moreButton setShowsTouchWhenHighlighted:YES];
[moreButton addTarget:self action:@selector(tapMoreButton:)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:moreButton];
//¸øÎÒÃǵİ´Å¥Ìí¼ÓÔ¼ÊøÀ´Èð´Å¥À´Õ¼ÂútoolView;
faceButton.translatesAutoresizingMaskIntoConstraints = NO;
moreButton.translatesAutoresizingMaskIntoConstraints = NO;
//Ìí¼ÓË®Æ½Ô¼Êø
NSArray *buttonH = [NSLayoutConstraint constraintsWithVisualFormat:@
"H:|[faceButton][moreButton(==faceButton)]|"
options:0 metrics:0 views:NSDictionaryOfVariableBindings(faceButton,moreButton)];
[self addConstraints:buttonH];
//Ìí¼Ó´¹Ö±Ô¼Êø
NSArray *button1V = [NSLayoutConstraint constraintsWithVisualFormat:@"
V:|[faceButton]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(faceButton)];
[self addConstraints:button1V];
NSArray *button2V = [NSLayoutConstraint constraintsWithVisualFormat:@
"V:|[moreButton]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(moreButton)];
[self addConstraints:button2V];
}
return self;
}
//½ÓÊÜ´«ÈëµÄ»Øµ÷
-(void) setToolIndex:(ToolIndex)toolBlock
{
self.myBlock = toolBlock;
}
//µã»÷±íÇé°´Å¥Òª»Øµ÷µÄ·½·¨
-(void) tapFaceButton: (id) sender
{
self.myBlock(1);
}
//µã»÷moreÒª»Øµ÷µÄ·½·¨
-(void) tapMoreButton: (id) sender
{
self.myBlock(2);
}
@end
|
´úÂë˵Ã÷£º
Ö÷ÒªÊǶÔblock»Øµ÷µÄÓ¦Óú͸øÏàÓ¦µÄ×é¼þÌí¼ÓÏàÓ¦µÄÔ¼Êø
4.MoreView×é¼þµÄ·â×°´úÂë¾Í²»ÍùÉÏÌùÀ²£¬ºÍÉÏÃæµÄÀàËÆ£¬ÏÂÃæÊǵ÷ÓÃMoreView×é¼þµÄÔËÐÐЧ¹û£¬ÓÐÐËȤµÄ¶ÁÕßÇë×ÔÐбàд,ÒÔÉϾÍÊÇÊÓͼ²¿·ÖµÄ´úÂëÁË

¶þ. Mode²¿·ÖµÄÄÚÈÝ
1.Ïȶ¨ÒåÎÒÃÇҪʹÓõÄÊý¾ÝÄ£ÐÍ£¬Êý¾ÝÄ£ÐÍÈçÏ£¬timeÊÇʹÓñíÇéµÄʱ¼ä£¬ÓÃÓÚÅÅÐò¡£

2.ÏÂÃæ±àдÎÒÃǵÄImageModelClassÀ࣬ÀïÃæ·â×°ÁËÎÒÃDzÙ×÷Êý¾ÝÒªÓõķ½·¨
ImageModelClass.hµÄ´úÂëÈçÏ£¬Ö÷ÒªÊÇÔ¤ÁôµÄ¶ÔÍâµÄ½Ó¿Ú£º
//
// ImageModelClass.h
// MyKeyBoard
//
// Created by ÇàÓñ·ü°¸ on 14-9-16.
// Copyright (c) 2014Äê Mrli. All rights reserved.
//
#import
#import
#import "HistoryImage.h"
@interface ImageModelClass : NSObject
//±£´æÊý¾Ý
-(void)save:(NSData *) image ImageText:(NSString *) imageText;
//²éѯËùÓеÄͼƬ
-(NSArray *) queryAll;
@end
|
ImageModelClass.mµÄ´úÂëÈçÏ£¬Ö÷ÒªÊÇÓÃCoreData¶ÔsqliteµÄ²Ù×÷£º
//
// ImageModelClass.m
// MyKeyBoard
//
// Created by ÇàÓñ·ü°¸ on 14-9-16.
// Copyright (c) 2014Äê Mrli. All rights reserved.
//
#import "ImageModelClass.h"
@interface ImageModelClass ()
@property (nonatomic, strong) NSManagedObjectContext *manager;
@end
@implementation ImageModelClass
- (instancetype)init
{
self = [super init];
if (self) {
//ͨ¹ýÉÏÏÂÎÄ»ñÈ¡manager
UIApplication *application = [UIApplication sharedApplication];
id delegate = application.delegate;
self.manager = [delegate managedObjectContext];
}
return self;
}
-(void)save:(NSData *)image ImageText:(NSString *)imageText
{
if (image != nil) {
NSArray *result = [self search:imageText];
HistoryImage *myImage;
if (result.count == 0)
{
myImage = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass
([HistoryImage class]) inManagedObjectContext:self.manager];
myImage.imageText = imageText;
myImage.headerImage = image;
myImage.time = [NSDate date];
}
else
{
myImage = result[0];
myImage.time = [NSDate date];
}
//´æ´¢ÊµÌå
NSError *error = nil;
if (![self.manager save:&error]) {
NSLog(@"±£´æ³ö´í%@", [error localizedDescription]);
}
}
}
//²éÕÒ
-(NSArray *)search:(NSString *) image
{
NSArray *result;
//н¨²éѯÌõ¼þ
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]
initWithEntityName:NSStringFromClass([HistoryImage class])];
//Ìí¼Óν´Ê
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"imageText=%@",image];
//°Ñν´Ê¸ørequest
[fetchRequest setPredicate:predicate];
//Ö´Ðвéѯ
NSError *error = nil;
result = [self.manager executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"²éѯ´íÎó£º%@", [error localizedDescription]);
}
return result;
}
//²éѯËùÓеÄ
-(NSArray *) queryAll
{
//н¨²éѯÌõ¼þ
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:NSStringFromClass([HistoryImage class])];
//Ìí¼ÓÅÅÐò¹æÔò
//¶¨ÒåÅÅÐò¹æÔò
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:NO];
//Ìí¼ÓÅÅÐò¹æÔò
[fetchRequest setSortDescriptors:@[sortDescriptor]];
//Ö´Ðвéѯ
NSError *error = nil;
NSArray *result = [self.manager executeFetchRequest:fetchRequest error:&error];
if (error) {
NSLog(@"²éѯ´íÎó£º%@", [error localizedDescription]);
}
return result;
}
@end
|
´úÂë˵Ã÷£º
1.±£´æÍ¼Æ¬Ê±ÏȲéÕÒͼƬÊÇ·ñ´æÔÚ£¬Èç¹û´æÔÚÔò¸üÐÂʱ¼ä£¬Èç¹û²»´æÔÚÔò²åÈëÊý¾Ý£¨Ð´µ½Õâ¸Ð¾õÏëÔÚÓÃHibernateд¶«Î÷£©¡£
Èý.Controller²¿·Ö£¬°ÑÉÏÃæµÄ×é¼þ½øÐÐ×é×°
1.MainViewController.mÖеÄÑÓÕ¹²¿·ÖµÄ´úÂëÈçÏ£º
@interface MainViewController ()
//×Ô¶¨Òå×é¼þ
@property (nonatomic, strong) ToolView *toolView;
@property (nonatomic, strong) FunctionView *functionView;
@property (nonatomic, strong) MoreView *moreView;
//ϵͳ×é¼þ
@property (strong, nonatomic) IBOutlet UITextView *myTextView;
@property (strong, nonatomic) NSDictionary *keyBoardDic;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) NSString *sendString;
//Êý¾Ýmodel
@property (strong, nonatomic) ImageModelClass *imageMode;
@property (strong, nonatomic)HistoryImage *tempImage;
@end
|
2.ÔÚviewDidLoadÖнøÐÐ×é¼þµÄ³õʼ»¯ºÍʵÏÖ×é¼þµÄBlock»Øµ÷£¬´úÂëÈçÏÂ
- (void)viewDidLoad
{
[super viewDidLoad];
//´ÓsqliteÖжÁÈ¡Êý¾Ý
self.imageMode = [[ImageModelClass alloc] init];
//ʵÀý»¯FunctionView
self.functionView = [[FunctionView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
self.functionView.backgroundColor = [UIColor blackColor];
//ÉèÖÃ×ÊÔ´¼ÓÔØµÄÎļþÃû
self.functionView.plistFileName = @"emoticons";
__weak __block MainViewController *copy_self = self;
//»ñȡͼƬ²¢ÏÔʾ
[self.functionView setFunctionBlock:^(UIImage *image, NSString *imageText)
{
NSString *str = [NSString stringWithFormat:@"%@%@",copy_self.myTextView.text, imageText];
copy_self.myTextView.text = str;
copy_self.imageView.image = image;
//°ÑʹÓùýµÄͼƬ´æÈësqlite
NSData *imageData = UIImagePNGRepresentation(image);
[copy_self.imageMode save:imageData ImageText:imageText];
}];
//ʵÀý»¯MoreView
self.moreView = [[MoreView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.moreView.backgroundColor = [UIColor blackColor];
[self.moreView setMoreBlock:^(NSInteger index) {
NSLog(@"MoreIndex = %d",index);
}];
//½øÐÐToolViewµÄʵÀý»¯
self.toolView = [[ToolView alloc] initWithFrame:CGRectZero];
self.toolView.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.toolView];
//¸øToolViewÌí¼ÓÔ¼Êø
//¿ªÆô×Ô¶¯²¼¾Ö
self.toolView.translatesAutoresizingMaskIntoConstraints = NO;
//Ë®Æ½Ô¼Êø
NSArray *toolHConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@
"H:|[_toolView]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_toolView)];
[self.view addConstraints:toolHConstraint];
//´¹Ö±Ô¼Êø
NSArray *toolVConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@
"V:[_toolView(44)]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_toolView)];
[self.view addConstraints:toolVConstraint];
//»Øµ÷toolViewÖеķ½·¨
[self.toolView setToolIndex:^(NSInteger index)
{
NSLog(@"%d", index);
switch (index) {
case 1:
[copy_self changeKeyboardToFunction];
break;
case 2:
[copy_self changeKeyboardToMore];
break;
default:
break;
}
}];
//µ±¼üÅ̳öÀ´µÄʱºòͨ¹ý֪ͨÀ´»ñÈ¡¼üÅ̵ÄÐÅÏ¢
//×¢²áΪ¼üÅ̵ļàÌý×Å
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyNotification:)
name:UIKeyboardWillChangeFrameNotification object:nil];
//¸ø¼üÅÌÌí¼Ódan
//TextViewµÄ¼üÅ̶¨ÖÆ»ØÊÕ°´Å¥
UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem * item1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(tapDone:)];
UIBarButtonItem * item2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem * item3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBar.items = @[item2,item1,item3];
self.myTextView.inputAccessoryView =toolBar;
}
|
3.µ±ºáÊúÆÁÄ»Çл»Ê±ÉèÖÃ×Ô¶¨Òå¼üÅ̵ĸ߶È
-(void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
//×ÝÆÁ
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
CGRect frame = self.functionView.frame;
frame.size.height = 216;
self.functionView.frame = frame;
self.moreView.frame = frame;
}
//ºáÆÁ
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
CGRect frame = self.functionView.frame;
frame.size.height = 150;
self.functionView.frame = frame;
self.moreView.frame = frame;
}
}
|
4.µ±¼üÅ̳öÀ´µÄʱºò£¬¸Ä±ätoolViewµÄλÖã¬Í¨¹ý¼üÅ̵Ä֪ͨÀ´ÊµÏÖ¡£µ±ºáÆÁµÄʱºò¼üÅ̵Ä×ø±êϵºÍÎÒÃǵ±Ç°µÄFrameµÄ×ø±êϵ²»Ò»ÑùËùÒÔµ±ºáÆÁʱµÃ×öÒ»×ø±êϵµÄת»»£¬´úÂëÈçÏ£»
//µ±¼üÅ̳öÀ´µÄʱºò¸Ä±ätoolViewµÄλÖ㨽ӵ½¼üÅ̳öÀ´µÄ֪ͨҪ×öµÄ·½·¨£©
-(void) keyNotification : (NSNotification *) notification
{
NSLog(@"%@", notification.userInfo);
self.keyBoardDic = notification.userInfo;
//»ñÈ¡¼üÅÌÒÆ¶¯ºóµÄ×ø±êµãµÄ×ø±êµã
CGRect rect = [self.keyBoardDic[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//°Ñ¼üÅ̵Ä×ø±êϵ¸Ä³Éµ±Ç°ÎÒÃÇwindowµÄ×ø±êϵ
CGRect r1 = [self.view convertRect:rect fromView:self.view.window];
[UIView animateWithDuration:[self.keyBoardDic[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
//¶¯»ÇúÏß
[UIView setAnimationCurve:[self.keyBoardDic[UIKeyboardAnimationCurveUserInfoKey] doubleValue]];
CGRect frame = self.toolView.frame;
frame.origin.y = r1.origin.y - frame.size.height;
//¸ù¾Ý¼üÅ̵ĸ߶ÈÀ´¸Ä±ätoolViewµÄ¸ß¶È
self.toolView.frame = frame;
}];
}
|
5.ϵͳ¼üÅ̺Í×Ô¶¨Òå¼üÅÌÇл»µÄ´úÂëÈçÏ£º
//Çл»¼üÅ̵ķ½·¨
-(void) changeKeyboardToFunction
{
if ([self.myTextView.inputView isEqual:self.functionView])
{
self.myTextView.inputView = nil;
[self.myTextView reloadInputViews];
}
else
{
self.myTextView.inputView = self.functionView;
[self.myTextView reloadInputViews];
}
if (![self.myTextView isFirstResponder])
{
[self.myTextView becomeFirstResponder];
}
}
|
ÒÔÉϾÍÊÇÉÏÃæÕ¹Ê¾Ð§¹ûµÄºËÐÄ´úÂëÁË£¬ÔÚ×öµÄʱºò¸Ð¾õÄѵãÔÚÓÚÈçºÎ½øÐÐÆÁÄ»ÊÊÅ䣬ÓÈÆäÊǵ±ÆÁÄ»ºá¹ýÀ´µÄʱºò¼üÅ̵Ä×ø±êϵºÍÎÒÃÇframeµÄ×ø±êϵ²»Í¬£¬µÃ×öÒ»¸öת»»¡£·¢±í²©¿ÍµÄÄ¿µÄÊÇÏëÆðµ½Å×שÒýÓñµÄ×÷Óã¬ÓкõĶ«Î÷Ï£Íû´ó¼ÒÏ໥½»Á÷һϡ£±ÊÕßˮƽÓÐÏÞÄÑÃâÓÐÆ«ÆÄÖ®´¦£¬»¶ÓÅúÆÀÖ¸Õý¡£
|