Skip to content

Commit

Permalink
搞定用两张图片来做subview然后设置到view上面。
Browse files Browse the repository at this point in the history
  • Loading branch information
iiiyu committed Mar 20, 2012
1 parent cbd70d8 commit 33de876
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 5 deletions.
18 changes: 17 additions & 1 deletion Draw Lock Login.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
C95FDA141518BE920056BD4A /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C95FDA121518BE920056BD4A /* MainStoryboard.storyboard */; };
C95FDA171518BE920056BD4A /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = C95FDA161518BE920056BD4A /* ViewController.m */; };
C95FDA1F1518BEB90056BD4A /* Draw Lock.m in Sources */ = {isa = PBXBuildFile; fileRef = C95FDA1E1518BEB90056BD4A /* Draw Lock.m */; };
C95FDA261518D9D20056BD4A /* dot_off.png in Resources */ = {isa = PBXBuildFile; fileRef = C95FDA241518D9D20056BD4A /* dot_off.png */; };
C95FDA271518D9D20056BD4A /* dot_on.png in Resources */ = {isa = PBXBuildFile; fileRef = C95FDA251518D9D20056BD4A /* dot_on.png */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -34,6 +36,8 @@
C95FDA161518BE920056BD4A /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
C95FDA1D1518BEB90056BD4A /* Draw Lock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Draw Lock.h"; sourceTree = "<group>"; };
C95FDA1E1518BEB90056BD4A /* Draw Lock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Draw Lock.m"; sourceTree = "<group>"; };
C95FDA241518D9D20056BD4A /* dot_off.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = dot_off.png; sourceTree = "<group>"; };
C95FDA251518D9D20056BD4A /* dot_on.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = dot_on.png; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -80,12 +84,13 @@
C95FDA061518BE920056BD4A /* Draw Lock Login */ = {
isa = PBXGroup;
children = (
C95FDA281518D9D90056BD4A /* image */,
C95FDA0F1518BE920056BD4A /* AppDelegate.h */,
C95FDA101518BE920056BD4A /* AppDelegate.m */,
C95FDA121518BE920056BD4A /* MainStoryboard.storyboard */,
C95FDA151518BE920056BD4A /* ViewController.h */,
C95FDA1D1518BEB90056BD4A /* Draw Lock.h */,
C95FDA1E1518BEB90056BD4A /* Draw Lock.m */,
C95FDA151518BE920056BD4A /* ViewController.h */,
C95FDA161518BE920056BD4A /* ViewController.m */,
C95FDA071518BE920056BD4A /* Supporting Files */,
);
Expand All @@ -103,6 +108,15 @@
name = "Supporting Files";
sourceTree = "<group>";
};
C95FDA281518D9D90056BD4A /* image */ = {
isa = PBXGroup;
children = (
C95FDA241518D9D20056BD4A /* dot_off.png */,
C95FDA251518D9D20056BD4A /* dot_on.png */,
);
name = image;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -155,6 +169,8 @@
files = (
C95FDA0B1518BE920056BD4A /* InfoPlist.strings in Resources */,
C95FDA141518BE920056BD4A /* MainStoryboard.storyboard in Resources */,
C95FDA261518D9D20056BD4A /* dot_off.png in Resources */,
C95FDA271518D9D20056BD4A /* dot_on.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
56 changes: 53 additions & 3 deletions Draw Lock Login/Draw Lock.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@

#import "Draw Lock.h"

#define MATRIX_SIZE 3

@implementation Draw_Lock


-(void)setup
{
self.backgroundColor = [UIColor darkGrayColor];
}

// 老头说过不直接调用initWithFrame 而是调用awakeFromNib来进行初始化
// 进过测试 initWithFrame也不会被调用
- (void)awakeFromNib
{
[self setup];
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(@"hello");
}
return self;
}
Expand Down Expand Up @@ -51,6 +67,35 @@ - (void)drawBackgroundAtColor:(CGFloat)red atGreen:(CGFloat)green atBlue:(CGFlo
UIGraphicsPopContext();
}

- (void)setSubviewImageButton
{
// 初始subview
for (int i = 0; i < MATRIX_SIZE; i++) {
for (int j = 0; j < MATRIX_SIZE; j++) {
UIImage *dotImage = [UIImage imageNamed:@"dot_off.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:dotImage
highlightedImage:[UIImage imageNamed:@"dot_on.png"]];
imageView.frame = CGRectMake(0, 0, dotImage.size.width, dotImage.size.height);
imageView.userInteractionEnabled = YES;
imageView.tag = j * MATRIX_SIZE + i + 1;
[self addSubview:imageView];
}
}

// 重新设置每一个subview的位置
int w = self.frame.size.width / MATRIX_SIZE;
int h = self.frame.size.height / MATRIX_SIZE;

int i = 0;
for (UIView *view in self.subviews)
if ([view isKindOfClass:[UIImageView class]]) {
int x = w * (i / MATRIX_SIZE) + w / 2;
int y = h * (i % MATRIX_SIZE) + h / 2;
view.center = CGPointMake(x, y);
i++;
}
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
Expand All @@ -59,10 +104,15 @@ - (void)drawRect:(CGRect)rect
// Drawing code

// 获取CGContext,注意UIKit里用的是一个专门的函数
CGContextRef context = UIGraphicsGetCurrentContext();
//
//CGContextRef context = UIGraphicsGetCurrentContext();

// draw background
//[self drawBackgroundAtColor:0.0 atGreen:0.0 atBlue:0.0 atAphla:0.8 inContext:context];

// draw background
[self drawBackgroundAtColor:0.0 atGreen:0.0 atBlue:0.0 atAphla:0.8 inContext:context];
// 蛋碎了 都想办法画了背景 然后发现view的背景可以这样设置 不过上面的是可以画成透明的
// self.backgroundColor = [UIColor darkGrayColor];
[self setSubviewImageButton];


}
Expand Down
34 changes: 34 additions & 0 deletions Draw Lock Login/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#import "ViewController.h"


//#define MATRIX_SIZE 3

@interface ViewController ()

@end
Expand All @@ -18,8 +21,39 @@ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
for (int i=0; i<MATRIX_SIZE; i++) {
for (int j=0; j<MATRIX_SIZE; j++) {
UIImage *dotImage = [UIImage imageNamed:@"dot_off.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:dotImage
highlightedImage:[UIImage imageNamed:@"dot_on.png"]];
imageView.frame = CGRectMake(0, 0, dotImage.size.width, dotImage.size.height);
imageView.userInteractionEnabled = YES;
imageView.tag = j*MATRIX_SIZE + i + 1;
[self.view addSubview:imageView];
}
}
*/

}

/*
- (void)viewWillLayoutSubviews {
int w = self.view.frame.size.width/MATRIX_SIZE;
int h = self.view.frame.size.height/MATRIX_SIZE;
int i=0;
for (UIView *view in self.view.subviews)
if ([view isKindOfClass:[UIImageView class]]) {
int x = w*(i/MATRIX_SIZE) + w/2;
int y = h*(i%MATRIX_SIZE) + h/2;
view.center = CGPointMake(x, y);
i++;
}
}
*/
- (void)viewDidUnload
{
[super viewDidUnload];
Expand Down
Binary file added Draw Lock Login/dot_off.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Draw Lock Login/dot_on.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1 +1 @@
主要拿来联系Quartz
主要拿来练习Quartz

0 comments on commit 33de876

Please sign in to comment.