IOS 学的容易----随机点名器内部实现

本文介绍了一种基于iOS的随机点名应用实现方案,详细展示了如何通过Objective-C语言进行界面布局及按钮交互的设计,并实现了触摸屏上的随机点名功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.面布局

#import "HuaMingCeViewController.h"
@implementation HuaMingCe
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSMutableArray *array = [[NSMutableArray alloc]
initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7",
@"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15",
@"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23",
@"24", @"25", @"16", @"27", @"28", @"29", @"30", @"31",
@"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39",
@"40", @"41", @"42", @"43", @"44", @"45", @"46", @"47",
@"48", @"49", @"50", @"51", @"52", @"53", @"54", @"55",
@"56", @"57", @"58",@" ",@" ", nil]; 


CGFloat width = frame.size.width;
CGFloat heiht = frame.size.height;

// 页面的布局
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 10; j++) {
self.button = [UIButton buttonWithType:(UIButtonTypeCustom
)];
self.button.frame =CGRectMake(3 + (width / 6) * i, 3 +
((heiht - 60) / 10) * j, 50, 40);
// self.button = [[UIButton alloc]
initWithFrame:CGRectMake(3 + (width / 6) * i, 3
+ ((heiht - 60) / 10) * j, 50, 40)];
self.button.backgroundColor = [UIColor greenColor];
[self.button setTintColor:[UIColor yellowColor]];
[self.button setTitle:array[6 * j + i] forState:
(UIControlStateNormal)];
[self addSubview:self.button];

//[self.button release];
}
}

// 开始的点击的按钮
self.button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
self.button1.frame = CGRectMake(0, 0, 100, 50);
// self.button1 = [[UIButton alloc]
initWithFrame:CGRectMake(0, 0, 100, 50)];
self.button1.backgroundColor = [UIColor grayColor];
[self.button1 setTintColor:[UIColor redColor]];
[self.button1 setTitle:@"开始点名" forState:(UIControlStateNormal)];
//[self.button1 addTarget:self action:@selector(Colick:)
forControlEvents:(UIControlEventTouchUpInside)];
self.button1.center = CGPointMake(width / 2, 590);
self.button1.showsTouchWhenHighlighted = YES;
}
return self;
}
}
*/
@end

随机点名的移动

#import <UIKit/UIKit.h>
@interface HuaMingCe : UIView
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UIButton *button1;
@end


#import <UIKit/UIKit.h>
@class TouchView;

// 自定义代理
@protocol TouchViewDeletage <NSObject>
- (void)Colick:(TouchView *)view;
@end
@interface TouchView : UIView
@property (nonatomic, assign) CGPoint startPoint;

// 设置代理对象
@property (nonatomic, assign) id<TouchViewDeletage>delegate;
@end
//
// TouchView.m
#import "TouchView.h"
@implementation TouchView
// 开始触发时 的信息 就⼀一次 所以触摸点和上⼀一个点的坐标⼀一样
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// for (int i = 0; i < 58; i++) {
// [_delegate Colick:self];
// }
[_delegate Colick:self];

// 点名随机颜色的调配
// self.backgroundColor = [UIColor colorWithRed:arc4random()
%256/ 255.0 green:arc4random()%256/ 255.0 blue:arc4random()
%256/ 255.0 alpha:1];

// 延时操作
// for (int i = 0; i < 58; i++) {
// for (int j = 0; j < 900000; j++) {
//
// }
//
//
// int n = arc4random() % 6;
// int m = arc4random() % 10;
// NSLog(@" 进⾏行中");
// // 触摸屏幕触摸的信息
// // 注意不能直接改变改变坐标 应该将中⼼心的坐标加上⿏鼠标的移动距离
// UITouch * touch = [touches anyObject];
// // 原先的位置中⼼心坐标
// CGPoint p1 = [touch previousLocationInView:self.window];
//
// NSLog(@"%@", NSStringFromCGPoint(p1));
// CGPoint p2 = [touch locationInView:self.window];
//
// NSLog(@"%@", NSStringFromCGPoint(p2));
// self.center = CGPointMake(48 + n * 56, 43 + m * 57.22);
// }
// // 触摸屏幕触摸的信息
// UITouch * touch = [touches anyObject];
// // NSLog(@"%@", touch); // 打印
// // 取出⼀一个点
// CGPoint p1 = [touch previousLocationInView:self];
// NSLog(@"%@", NSStringFromCGPoint(p1));
//
//
//
// CGPoint p2 = [touch locationInView:self];
// self.startPoint = p1;
// NSLog(@"%@", NSStringFromCGPoint(p2));
// NSLog(@"开始触摸");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
Page 7 of 10
AppDelegate.m 15/9/1 下午7:25
{
// [_delegate Colick:self];
// 随机改变背景的颜⾊色
// self.backgroundColor = [UIColor colorWithRed:arc4random()%256/
255.0 green:arc4random()%256/ 255.0 blue:arc4random()%256/
255.0 alpha:1];
// for (int i = 0; i < 58; i++) {
// for (int j = 0; j < 900000; j++) {
//
// }
//
//
// int n = arc4random() % 6;
// int m = arc4random() % 10;
// NSLog(@" 进⾏行中");
// // 触摸屏幕触摸的信息
// // 注意不能直接改变改变坐标 应该将中⼼心的坐标加上⿏鼠标的移动距离
// UITouch * touch = [touches anyObject];
// // 原先的位置中⼼心坐标
// CGPoint p1 = [touch previousLocationInView:self.window];
//
// NSLog(@"%@", NSStringFromCGPoint(p1));
// CGPoint p2 = [touch locationInView:self.window];
//
// NSLog(@"%@", NSStringFromCGPoint(p2));
// self.center = CGPointMake(48 + n * 56, 43 + m * 57.22);
// }

}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// [_delegate Colick:self];
NSLog(@"结束");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"终⽌止");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during
animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值