微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IOS考试题2

IOS考试题2

在这里插入图片描述

考点,考察UI控件的坐标获取,动画的使用等知识的使用,OC能写出来,swift就能写出来,swiftUI略过,国内使用率10%(截止今天)
oc写法

//
//  ViewController.m
//  002-考试题2oc1
//
//  Created by 鲁军 on 2021/5/30.
//

#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *itcastButton;
@property (weak, nonatomic) IBOutlet UIView *underline;
@property(nonatomic,weak)UIButton *selectedButton;
@end
@implementation ViewController
- (IBAction)buttonClick:(UIButton *)sender {
    // NSLog(@"%@",sender.currentTitle);
    if(self.selectedButton!=nil){
        self.selectedButton.selected = NO;
    }
    sender.selected = YES;
    self.selectedButton = sender;
    [UIView animateWithDuration:0.25 animations:^{
        CGFloat underlineX = CGRectGetMinX(sender.frame);
        CGFloat underlineY = CGRectGetMaxY(sender.frame);
        CGFloat underlineW = sender.frame.size.width;
        CGFloat underlineH = self.underline.frame.size.height;
        self.underline.frame = CGRectMake(underlineX, underlineY, underlineW, underlineH);
       }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
    [UIView setAnimationsEnabled:NO];
    [self buttonClick:self.itcastButton];
    [UIView setAnimationsEnabled:YES];
}
@end

swift写法

//
//  ViewController.swift
//  02-考试题2
//
//  Created by 鲁军 on 2021/5/25.
//
import UIKit
class ViewController: UIViewController {
    private var selectedButton: UIButton?
    @IBOutlet weak var underline: UIView!
    @IBOutlet weak var itcastButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
   
    @IBAction func buttonClick(sender: UIButton) {
        //debugPrint(sender.currentTitle as Any)
           
        if (selectedButton != nil) {
                selectedButton!.isSelected = false
            }
            sender.isSelected = true
            self.selectedButton = sender
            UIView.animate(withDuration: 0.25, animations: {
                let underlineX = sender.frame.minX
                let underlineY = sender.frame.maxY
                let underlineW = sender.frame.width
                let underlineH = self.underline.frame.height
            self.underline.frame = CGRect(x: underlineX, y: underlineY, width: underlineW, height: underlineH)
        })
    }
    override func viewDidAppear(_ animated: Bool)  {
        UIView.setAnimationsEnabled(false)
        buttonClick(sender: self.itcastButton)
        UIView.setAnimationsEnabled(true)
    }
}


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐