iOS编程:学习篇(十二)

向屏幕中添加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];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值