向屏幕中添加UILabel对象
在HypnoNerd中,随机出现在屏幕上UILabel对象。
- (void)drawHypnoticMessage:(NSString *)message{
for (int i = 0; i < 20; i++) {
UILabel *messageLabel = [[UILabel alloc] init];
// 设置UILabel对象的文字和颜色
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.textColor = [UIColor whiteColor];
messageLabel.text = message;
// 根据需要显示的文字调整UILabel对象的大小
[messageLabel sizeToFit];
// 随机获取x坐标
// 是UILabel对象的宽度不超出HypnosisViewController的view宽度
int width = (int)(self.view.bounds.size.width -
messageLabel.bounds.size.width);
int x = arc4random() % width;
// 随机获取y坐标
int height = (int)(self.view.bounds.size.height -
messageLabel.bounds.size.height);
int y = arc4random() % height;
// 设置UILabel对象的frame
CGRect frame = messageLabel.frame;
frame.origin = CGPointMake(x, y);
messageLabel.frame = frame;
// 将UILabel对象添加到HypnosisViewController的view中
[self.view addSubview:messageLabel];
}
}
运动效果
iOS设备内嵌了许多功能强大的传感器,如加速传感器,磁场传感器和三轴陀螺仪。
视差(parallax):想像自己坐在一个飞驰的汽车中,这时向车船外望去,会发现远处景物的倒退速度比近处的要慢的多,这是大脑堆空间和速度差异产生的一种错觉。
应用可以通过UIInterpolatingMotionEffect类实现该效果,创建一个UIInterpolatingMotionEffect对象,设置其方向,健路径和相对最小/最大值,再将其添加到某个视图上,该视图就能获得相应的视差效果。
UIInterpolatingMotionEffect *motionEffect;
motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffect.minimumRelativeValue = @(-25);
motionEffect.maximumRelativeValue = @(25);
[messageLabel addMotionEffect:motionEffect];
motionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
motionEffect.minimumRelativeValue = @(-25);
motionEffect.maximumRelativeValue = @(25);
[messageLabel addMotionEffect:motionEffect];