1.今天在公司中遇到一个问题:想到一个好的想法
1 // 2 // CloudView.m 3 // Test 4 // 5 // Created by zhangmh on 12-7-9. 6 // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. 7 // 8 9 #import "CloudView.h" 10 11 @interface CloudView() 12 13 @property (nonatomic) CGFloat top; 14 @property (nonatomic) CGFloat bottom; 15 @property (nonatomic) CGFloat left; 16 @property (nonatomic) CGFloat right; 17 18 - (void)animationUpdate; 19 20 21 - (CGFloat)limitSpeedbettowinMINandMAX:(CGFloat)speedValue; 22 @end 23 24 @implementation CloudView 25 26 @synthesize delegate; 27 28 @synthesize top; 29 @synthesize bottom; 30 @synthesize left; 31 @synthesize right; 32 33 34 #pragma mark - 35 #pragma mark --初始化方法-- 36 - (id)initWithFrame:(CGRect)frame andNodeCount:(NSInteger)nodeCount 37 { 38 self = [super initWithFrame:frame]; 39 if (self) { 40 41 CGSize nodeSize = CGSizeMake(25, 25); 42 43 speedMAX = 2.0f;//最大移动速度 44 speedMIN = -2.0f;//最小移动速度 45 46 self.left = frame.origin.x - nodeSize.width; 47 self.right = frame.origin.x + frame.size.width; 48 self.top = frame.origin.y - nodeSize.height; 49 self.bottom = frame.origin.y + frame.size.height; 50 51 oldtouchPoint = CGPointMake(-1000, -1000); 52 53 for (NSInteger i = 0;i < nodeCount; i ++) { 54 55 CGFloat x = arc4random()%(int)(self.right - nodeSize.width); 56 CGFloat y = arc4random()%(int)(self.bottom - nodeSize.height); 57 58 CloudButton *cloudButton = [[CloudButton alloc] initWithFrame:CGRectMake(x, 59 y, 60 nodeSize.width, 61 nodeSize.height)]; 62 63 cloudButton.tag = i; 64 cloudButton.userInteractionEnabled = NO; 65 66 [cloudButton setTitle:[NSString stringWithFormat:@"%d",i] 67 forState:UIControlStateNormal]; 68 69 [cloudButton addTarget:delegate 70 action:@selector(didSelectedNodeButton:) 71 forControlEvents:UIControlEventTouchUpInside]; 72 73 float fontSize = arc4random()%20+8; 74 75 cloudButton.titleLabel.font = [UIFont systemFontOfSize:fontSize]; 76 cloudButton.alpha = fontSize/25; 77 78 NSLog(@"%d.alpha==%f",i,cloudButton.alpha); 79 80 [self addSubview:cloudButton]; 81 [cloudButton release]; 82 } 83 84 animationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 85 target:self 86 selector:@selector(animationUpdate) 87 userInfo:nil 88 repeats:YES]; 89 [animationTimer setFireDate:[NSDate distantPast]]; 90 } 91 return self; 92 } 93 94 #pragma mark - 95 #pragma mark --触摸事件-- 96 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 97 { 98 UITouch *theTouch = [touches anyObject]; 99 CGPoint currentPoint = [theTouch locationInView:self]; 100 oldtouchPoint = CGPointMake(currentPoint.x, currentPoint.y); 101 102 for (int i=0; i<self.subviews.count; i++) { 103 CloudButton *cloud = [self.subviews objectAtIndex:i]; 104 cloud.userInteractionEnabled = NO; 105 } 106 } 107 108 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 109 { 110 UITouch *theTouch = [touches anyObject]; 111 CGPoint currentPoint = [theTouch locationInView:self]; 112 if (oldtouchPoint.x != -1000 && oldtouchPoint.y != -1000) { 113 114 for (int i=0; i<self.subviews.count; i++) { 115 CloudButton *cloud = [self.subviews objectAtIndex:i]; 116 117 cloud.distanceX = [self limitSpeedbettowinMINandMAX:(currentPoint.x - oldtouchPoint.x)]; 118 119 cloud.distanceY = [self limitSpeedbettowinMINandMAX:(currentPoint.y - oldtouchPoint.y)]; 120 121 CGFloat distance = sqrt(cloud.distanceX*cloud.distanceX+ 122 cloud.distanceY*cloud.distanceY); 123 if (distance!=0) { 124 125 if (0.001 <= cloud.acceleration) { 126 cloud.acceleration = (arc4random()%100)/200.0f; 127 } 128 129 } 130 131 } 132 } 133 } 134 135 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 136 { 137 for (int i=0; i<self.subviews.count; i++) { 138 CloudButton *cloud = [self.subviews objectAtIndex:i]; 139 cloud.userInteractionEnabled = YES; 140 } 141 } 142 #pragma mark - 143 144 #pragma mark - 145 #pragma mark --动画事件-- 146 - (void)animationUpdate 147 { 148 for (int i=0; i<self.subviews.count; i++) { 149 150 CloudButton *cloud = [self.subviews objectAtIndex:i]; 151 152 cloud.center = CGPointMake(cloud.center.x + cloud.distanceX*cloud.acceleration, 153 cloud.center.y + cloud.distanceY*cloud.acceleration); 154 if (cloud.center.x < self.left) { 155 cloud.center = CGPointMake(self.right,self.bottom - cloud.center.y); 156 } 157 158 if (cloud.center.x > self.right) { 159 cloud.center = CGPointMake(self.left, self.bottom - cloud.center.y); 160 } 161 162 if (cloud.center.y < self.top){ 163 cloud.center = CGPointMake(self.right - cloud.center.x, self.bottom); 164 } 165 166 if (cloud.center.y > self.bottom) { 167 168 cloud.center = CGPointMake(self.right - cloud.center.x, self.top); 169 } 170 171 cloud.acceleration -= 0.001; 172 173 if (cloud.acceleration < 0.001) { 174 cloud.acceleration = 0.001; 175 } 176 177 } 178 } 179 180 181 182 - (CGFloat)limitSpeedbettowinMINandMAX:(CGFloat)speedValue 183 { 184 if (speedValue > speedMAX) { 185 speedValue = speedMAX; 186 } 187 188 if (speedValue < speedMIN) { 189 speedValue = speedMIN; 190 } 191 192 return speedValue; 193 } 194 #pragma mark - 195 #pragma mark - 196 /* 197 // Only override drawRect: if you perform custom drawing. 198 // An empty implementation adversely affects performance during animation. 199 - (void)drawRect:(CGRect)rect 200 { 201 // Drawing code 202 } 203 */ 204 205 @end
我么想
时间: 2024-11-11 12:18:59