通过按钮 启动NSTimer定时器 控制视图view移动效果
2017-01-15 13:20
441 查看
创建启动和停止 定时器的按钮 ,和添加视图:
定义 定时器触发事件 ,移动视图的效果:
定义两个 按钮触发事件:
#pragma mark - NSTimer定时器 - (void)setTimeCreat{ //创建 启动定时器按钮 UIButton* btn =[UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame =CGRectMake(100, 100, 80, 40); [btn setTitle:@"启动定时器" forState:UIControlStateNormal]; //给按钮添加 事件 [btn addTarget:self action:@selector(StartTime) forControlEvents:UIControlEventTouchUpInside]; //添加按钮 [self.view addSubview:btn]; //创建 启动定时器按钮 UIButton* btnStop =[UIButton buttonWithType:UIButtonTypeRoundedRect]; btnStop.frame =CGRectMake(100, 200, 80, 40); [btnStop setTitle:@"停止定时器" forState:UIControlStateNormal]; //给按钮添加 事件 [btnStop addTarget:self action:@selector(StopTime) forControlEvents:UIControlEventTouchUpInside]; //添加按钮 [self.view addSubview:btnStop]; //添加视图 UIView* view =[[UIView alloc] init]; view.frame =CGRectMake(0, 0, 80, 80); view.backgroundColor =[UIColor orangeColor]; [self.view addSubview:view]; //设置标签值 ,通过父视图 对象以及 子视图的标签值 可以获得子视图对象 view.tag =101; }
定义 定时器触发事件 ,移动视图的效果:
//定时器事件 - (void)updateTimer:(NSTimer *)timer{ //定时器本身作为参数 NSLog(@"name= %@" ,timer.userInfo); //tag从100开始 UIView* view =[self.view viewWithTag:101]; view.frame =CGRectMake(view.frame.origin.x +1, view.frame.origin.y +1, 80, 80); }
定义两个 按钮触发事件:
//启动定时器 按钮触发 ,多按几次可以达到 加速的效果 - (void)StartTime{ //给定时器添加 触发事件 ,时间戳TimeInterval 以秒为单位 _timer =[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer:) userInfo:@"小鑫" repeats:YES]; } - (void)StopTime{ if(_timer != nil){ //停止定时器 [_timer invalidate]; } }
相关文章推荐
- 滚动UIScrollView中子视图(按钮),实现 UIScrollView 滚动效果
- iPhone开发之UIScrollView滚动组件的使用(二) 拖线实现按钮控制大图移动利用contentOffset属性
- Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
- Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
- 自定义TextView跑马灯效果可控制启动/停止/速度
- iPhone开发之UIScrollView滚动组件的使用(三) 拖线实现按钮控制大图移动之contentInsets增加内容边距
- android TV 通过按键控制RecyclerView中的item的选中,移动,点击功能
- Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
- JS控制点击按钮图片向左或向右循环移动滚动效果
- 按钮控制ViewPager的左右翻页,保留原有的动画效果
- 自定义TextView跑马灯效果,可控制启动,停止,和速度(含源码)
- 基于Proteus+8051汇编应用实例系列之二--通过按钮来控制LED流水灯移动
- 通过electron的webView使用按钮控制前进后退
- 自定义TextView跑马灯效果可控制启动/停止/速度
- 手势检测实现相册的左右滑动(并加上移动与旋转的特效) 首先,activity_main.xml里,使用ViewFlipper组件(可使用动画控制多个组件之间的切换效果) <?xml version="
- [Phonegap+Sencha Touch] 移动开发48 扩展的NavigationView控件,支持根据不同的View显示不同的顶栏按钮、不同的屏幕方向,增加视图缓存机制
- 【移动开发】为ViewGroup的子视图添加动画效果
- 通过按钮来控制模型的移动与镜头的跟随
- Android中通过ViewHelper.setTranslationY实现View移动控制(NineOldAndroids开源项目)
- iOS小效果(0)按钮控制图片的移动的方向