方法总结
1.
判断某个点在一定范围内
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect xxxFrame = XXX.frame;
if (CGRectContainsPoint(xxxFrame, point))
{
return XXX;
}
return nil;
}
2、获取触摸点
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
//获取触摸点在根视图的位置
CGPoint location = [touch locationInView:self.view];
}
3.
#pragma mark - 转场动画 — 界面的跳转
CATransition *ani = [CATransition animation];
ani.type = @"rippleEffect”;水波纹
ani.duration = 2.0f;
ani.subtype = kCATransitionFromBottom;
[self.navigationController.view.layer addAnimation:ani forKey:nil];
[self.navigationController pushViewController:[[NextViewController alloc] init] animated:NO];
4.
#pragma mark - 摇一摇截屏
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
UIGraphicsBeginImageContext(self.view.bounds.size);
//获取当前上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//将当前图片对象渲染到上下文种
[self.view.layer renderInContext:context];
UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndPDFContext();
NSData *data = UIImagePNGRepresentation(captureImage);
//将截屏图片保存到桌面
[data writeToFile:@"/Users/qianfeng/Desktop/capture.png" atomically:YES];
}
5.POP 实现的秒表计数器
POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"countdown" initializer:^(POPMutableAnimatableProperty *prop) {
prop.writeBlock = ^(id obj, const CGFloat values[]) {
UILabel *lable = (UILabel*)obj;
label.text = [NSString stringWithFormat:@"d:d:d",(int)values[0]/60,(int)values[0]%60,(int)(values[0]*100)0];
};
// prop.threshold = 0.01f;
}];
POPBasicAnimation *anBasic = [POPBasicAnimation linearAnimation]; //秒表当然必须是线性的时间函数
anBasic.property = prop; //自定义属性
anBasic.fromValue = @(0); //从0开始
anBasic.toValue = @(3*60); //180秒
anBasic.duration = 3*60; //持续3分钟
anBasic.beginTime = CACurrentMediaTime() + 1.0f; //延迟1秒开始
[label pop_addAnimation:anBasic forKey:@"countdown"];
6.
// 为了防止线程中断
// 设置当前运行循环模式
// [NSDate distantFuture] 未来的某一个时间
while (!_isFinished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
7可以全局使用Appdelegate
#import "AppDelegate.h"
extern AppDelegate *app;
#import "AppDelegate.m”
AppDelegate *app=nil;
@interface AppDelegate ()
@end
8.
// 添加键盘显示和隐藏的监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
#pragma mark - Notification
#pragma mark 键盘弹起
- (void)keyboardWillShow:(NSNotification *)noti {
// 键盘弹起时长
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 获取键盘的高度
CGFloat keyboarHeight = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
// 移动inputView
[UIView animateWithDuration:duration animations:^{
_inputView.transform = CGAffineTransformMakeTranslation(0, -keyboarHeight);
}];
// 改变tableViewFrame
[self changeTableFrame:NO];
}
#pragma mark 键盘隐藏
- (void)keyboardWillHide:(NSNotification *)noti {
// 键盘弹起时长
CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 移动inputView
[UIView animateWithDuration:duration animations:^{
_inputView.transform = CGAffineTransformIdentity;
}];
// 改变tableViewFrame
[self changeTableFrame:YES];
}
//取消编辑事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 所有的视图都是加载在window之上.
[[[UIApplication sharedApplication].delegate window] endEditing:YES];
}
9
#pragma mark ============== 产生随机订单号 ==============
- (NSString *)generateTradeNO
{
static int kNumber = 15;
NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
//通常的做法是 以这样一句代码srand((unsigned) time(NULL));来取代,这样将使得种子为一个不固定的数, 这样产生的随机数就不会每次执行都一样了
srand((unsigned)time(0));
for (int i = 0; i < kNumber; i++)
{
unsigned index = rand() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
[resultStr appendString:oneStr];
}
return resultStr;
}