UI-事件处理
发布日期:2022-02-08 18:03:23 浏览次数:28 分类:技术文章

本文共 3284 字,大约阅读时间需要 10 分钟。

补充:按钮字符串替换

//  按钮在  显示和隐藏两个字符串之间替换   点击一下  替换一下- (void)buttonAction:(UIButton *)sender {    #pragma 方法一://    if (sender.selected) { // selected 的默认值是NO 第一次点击无效果//        [sender setTitle:@"隐藏" forState:UIControlStateNormal];        sender.selected = NO;//    }else {//        [sender setTitle:@"显示" forState:UIControlStateNormal];        sender.selected = YES;//    }//    sender.selected = !sender.selected; // BOOL值可以取非    #pragma 方法二://    sender.selected = !sender.selected; // BOOL值可以取非//    sender.selected ? [sender setTitle:@"隐藏" forState:UIControlStateNormal] : [sender setTitle:@"显示" forState:UIControlStateNormal];    #pragma 方法三:    sender.selected = !sender.selected; // BOOL值可以取非    [sender setTitle:sender.selected?@"隐藏":@"显示" forState:UIControlStateNormal];    }

UIViewController是MVC设计模式的核心。MVC是一个框架级的设计模式。

M是Model主要⽤用于建⽴数据模型(即数据的结构)

V是View,我们能看到的所有控件都view,view主要的功能是展示数据。

C是控制器,主要是控制M和V的通信。

View:在自定义视图中创建控件,初始化控件,设置一些基本的属性。

Controller:在控制器中写一些视图的触发事件

1. 触摸事件:

#pragma Touch--触摸的时候开始发送消息  -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 集合里存储对象---NSSet NSLog(@"触摸------开始"); } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"over"); } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint point1 = [touch locationInView:self.rootV.touchView]; CGPoint point2 = [touch previousLocationInView:self.rootV.touchView]; CGPoint center = self.rootV.touchView.center; CGFloat x = point1.x - point2.x; CGFloat y = point1.y - point2.y;  // 判断范围......最大的横纵坐标值 CGFloat MaxX = [UIScreen mainScreen].bounds.size.width; CGFloat MaxY = [UIScreen mainScreen].bounds.size.height;  CGFloat radius = self.rootV.touchView.layer.cornerRadius;// 半径  // 视图的四个边界值 CGFloat leftX = center.x + x - radius; CGFloat rightX = center.x + x + radius; CGFloat topY = center.y + y - radius; CGFloat downY = center.y + y + radius;  if (rightX <= MaxX && leftX >= 0 ) { if (topY >= 0 && downY <= MaxY) { self.rootV.touchView.center = CGPointMake(center.x + x, center.y + y); } } }  -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }

userInteractionEnabled属性:是控制是否阻断响应的属性(BOOL)

UITouch的两个方法:

- (CGPoint)locationInView:(UIView *)view;----返回当前触摸点的坐标

- (CGPoint)previousLocationInView:(UIView *)view;----返回当前的触摸点之前一个点的坐标(看API文档  有解释)

2. 响应者链:

响应事件查询顺序:UIApplication-->window-->viewController-->view-->所有的子视图

事件处理顺序:被点击的子视图-->view-->viewController-->window-->UIApplication-->事件作废

3.控制晃动(运动事件)

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{    //检测到摇动    NSLog(@"这个是检测有没有摇晃的   反正你也看不到 →_→  →_→");    }- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{    NSLog(@"摇晃结束~ ~ ~");    if (event.subtype == UIEventSubtypeMotionShake) {        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"TEST" message:@"机智如我" delegate:nil cancelButtonTitle:@"我本来就机智" otherButtonTitles:@"我确定我很机智",nil];        [alertView show];    }}- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{    //摇动取消}

4. 屏幕旋转:

//获得当前方向 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation  { return toInterfaceOrientation == UIDeviceOrientationPortrait; } //开启旋转 - (BOOL)shouldAutorotate  { return YES; }

转载地址:https://blog.csdn.net/Evelynzn/article/details/49206593 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:UI-UITextField UIButton
下一篇:UI-设计模式 手势处理

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月23日 08时50分48秒