s
AppDelegate.m文件中简单的初始化
//
// RootViewController.m
// test
//
// Created by kuangkuang on 13-9-26.
// Copyright (c) 2013年 kuangkuang. All rights reserved.
//
#import "RootViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Boundblock.h"
@interface RootViewController ()
@property(nonatomic,retain)UIView *block;
@property(nonatomic,retain)Boundblock *boundblock;
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)btncilck:(UIButton *)sender{
[self.boundblock moveBlockTo:sender.frame.origin.x];
}
-(void)buildlayout{
self.view.backgroundColor=[UIColor colorWithRed:0.4 green:0.3 blue:0.8 alpha:0.6];
#define BTN_COUNT 3
#define BTN_WIDTH 320/BTN_COUNT
for (int i=0; i<BTN_COUNT; i++) {
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(BTN_WIDTH*i, CGRectGetHeight(self.view.bounds)-40, BTN_WIDTH, 40);
[btn setTitle:[NSString stringWithFormat:@"btn%d",i+1] forState:UIControlStateNormal];
//btn.backgroundColor=[UIColor clearColor];
//[btn setBackgroundColor:[UIColor redColor]];
btn.layer.borderColor=[UIColor redColor].CGColor;
btn.layer.borderWidth=0.7;
[btn addTarget:self action:@selector(btncilck:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
self.boundblock=[[Boundblock alloc]initWithFrame:CGRectMake(0,CGRectGetHeight(self.view.bounds)-50,320,10) blockCount:BTN_COUNT];
self.boundblock.backgroundColor=[UIColor whiteColor];
[self.view addSubview:self.boundblock];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self buildlayout];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface Boundblock : UIView
-(id)initWithFrame:(CGRect)frame blockCount:(NSInteger)count;
-(void)moveBlockTo:(CGFloat)x;
@end
//
// Boundblock.m
// test
//
// Created by kuangkuang on 13-9-26.
// Copyright (c) 2013年 kuangkuang. All rights reserved.
//
#import "Boundblock.h"
#import "RootViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface Boundblock ()
@property(nonatomic,retain)UIView *block;
@end
@implementation Boundblock
-(void)moveBlockTo:(CGFloat)x{
__block CGRect targetFrame=self.block.frame;
BOOL directionRight=targetFrame.origin.x<x;
targetFrame.origin.x=x+(directionRight?20:-20);
[UIView animateWithDuration:0.1 animations:^{
self.block.frame=targetFrame;
}completion:^(BOOL finished) {
targetFrame.origin.x+=directionRight?-40:40;
[UIView animateWithDuration:0.1 animations:^{
self.block.frame=targetFrame;
} completion:^(BOOL finished) {
targetFrame.origin.x+=directionRight?20:-20;
[UIView animateWithDuration:0.1 animations:^{
self.block.frame=targetFrame;
}];
}];
}];
}
-(id)initWithFrame:(CGRect)frame blockCount:(NSInteger)count{
self=[super initWithFrame:frame];
if (self) {
UIView *bglayer=[[UIView alloc]initWithFrame:self.bounds];
bglayer.backgroundColor=[UIColor darkGrayColor];
bglayer.layer.opacity=0.4;
[self addSubview:bglayer];
self.block=[[UIView alloc]initWithFrame:CGRectMake(0,0,frame.size.width/count,frame.size.height)];
self.block.backgroundColor=[UIColor orangeColor];
self.block.layer.opacity=1;
[self addSubview:self.block];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
return [self initWithFrame:frame blockCount:5];
}
@end