- (void)viewDidLoad { [super viewDidLoad]; // 1.监听通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.accountField]; } //监听结束需要移除- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
#import "TutorialProjectViewController.h"@implementation TutorialProjectViewController@synthesize threadValueLabel, threadProgressView, testValueLabel, threadStartButton;// ------ Tutorial code starts here ------- (IBAction) startThreaonPressed:(UIButton *)sender { threadStartButton.hidden = YES; threadValueLabel.text = @"0"; threadProgressView.progress = 0.0; [NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil]; }- (void)startTheBackgroundJob { //注册一个监听者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Update) name:@"UpdateTableView" object:nil]; NSLog(@"1111111111"); NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time [NSThread sleepForTimeInterval:3]; [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO]; [pool release]; }- (void)makeMyProgressBarMoving { xx++;// NSLog(@"22222222 %i",xx); float actual = [threadProgressView progress]; threadValueLabel.text = [NSString stringWithFormat:@"%.2f", actual]; if (actual < 1) { threadProgressView.progress = actual + 0.01; [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO]; } if (xx==5||xx==15) { //发出一个消息。此前注册的监听者可以发现这个消息,并且出发相应的方法 [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateTableView" object:nil]; NSLog(@"xxxxxxxxxxxxxx==3"); } if (xx==10) { //取消监听。 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UpdateTableView" object:nil]; } else threadStartButton.hidden = NO; }//受到监听的消息时,调用这个函数-(void)Update{ NSLog(@"----------sdafs");}- (IBAction) testValueSliderChanged:(UISlider *)sender { testValueLabel.text = [NSString stringWithFormat:@"%.2f", sender.value]; }// ------ Tutorial code ends here ------- (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.}- (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;}// This function is for button which takes you to the xprogress.com website- (IBAction) runXprogressComButton: (id) sender { NSURL *url = [ [ NSURL alloc ] initWithString: @"http://www.xprogress.com/" ]; [[UIApplication sharedApplication] openURL:url];}- (void)dealloc { // ------ Tutorial code starts here ------ [threadValueLabel release]; [threadProgressView release]; [threadStartButton release]; [testValueLabel release]; // ------ Tutorial code ends here ------ [super dealloc];}@end
- UITapGestureRecognizer
- UIPinchGestureRecognizer
- UIRotationGestureRecognizer
- UISwipeGestureRecognizer
- UIPanGestureRecognizer
- UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。
// 定义一个 recognizer, 并加到需要偵測该手势的 UIView 元件上
-
(void)viewDidLoad {
UISwipeGestureRecognizer
*
recognizer;
// handleSwipeFrom 是偵測到手势,所要呼叫的方法
recognizer
= [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom)];
// 不同的 Recognizer 有不同的实体变数
// 例如 SwipeGesture 可以指定方向
// 而 TapGesture 則可以指定次數
recognizer
.
direction
= UISwipeGestureRecognizerDirectionUp
[
self
.
view
addGestureRecognizer:recognizer];
[
recognizer
release];
}
-
(void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
// 触发手勢事件后,在这里作些事情
// 底下是刪除手势的方法
[
self
.
view
removeGestureRecognizer:recognizer];
}
问题來了。有些手势其实是互相关联的,例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。当一個 UIView 同时添加兩个相关联的手势时,到底我这一下手指头按的要算是 Tap 还是 LongPress?如果照預设作法来看,只要「先滿足条件」的就会跳出并呼叫对应方法,举例来说,如果同时注册了 Pan 和 Swipe,只要手指头一移动就会触发 Pan 然后跳出,因而永远都不會发生 Swipe;单点与双点的情形也是一样,永远都只会触发单点,不會有双点。
那么这个问题有解吗?答案是肯定的,
UIGestureRecognizer
有个方法叫做
requireGestureRecognizerToFail
,他可以指定某一个 recognizer,即便自己已经滿足條件了,也不會立刻触发,会等到该指定的 recognizer 确定失败之后才触发。以同时支持单点与双点的手势为例,代码如下:
-
(void)viewDidLoad {
// 单击的 Recognizer
UITapGestureRecognizer
*
singleRecognizer;
singleRecognizer
= [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)];
singleTapRecognizer
.
numberOfTapsRequired
= 1; // 单击
[
self
.
view
addGestureRecognizer:singleRecognizer];
// 双击的 Recognizer
UITapGestureRecognizer
*
double;
doubleRecognizer
= [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)];
doubleTapRecognizer
.
numberOfTapsRequired
= 2; // 双击
[
self
.
view
addGestureRecognizer:doubleRecognizer];
// 关键在这一行,如果双击确定偵測失败才會触发单击
[
singleRecognizer
requireGestureRecognizerToFail:doubleRecognizer];
[
singleRecognizer
release];
[
doubleRecognizer
release];
}
学习ios 重要还是要理清楚思路 在做或者看老师代码的时候 自己多想想为什么 不要自己看着就抄 另外还是要推荐一下 蓝懿IOS这个培训机构 和刘国斌老师刘国斌老师还是很有名气的,听朋友说刘老师成立了蓝懿iOS,,老师讲课方式很独特,能够尽量让每个人都能弄明白,有的比较难懂的地方,如果有的地方还是不懂得话,老师会换个其它方法再讲解,这对于我们这些学习iOS的同学是非常好的,多种方式的讲解会理解得更全面,这个必须得给个赞,嘻嘻,还有就是这里的学习环境很好,很安静,可以很安心的学习,安静的环境是学习的基础,小班讲课,每个班20几个学生,学习氛围非常好,每天都学到9点多才离开教室,练习的时间很充裕,而且如果在练习的过程中有什么困难,随时可以向老师求助,不像其它机构,通过视频教学,有的甚至学完之后都看不到讲师本人,问点问题都不方便,这就是蓝懿与其它机构的区别,相信在刘国斌老师的细心指导下,每个蓝懿学员都能找到满意的工作,加油!
写博客第七十九天;
QQ:565803433
-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardDidHideNotification object:nil];
- 在键盘将要出现和隐藏的回调中,加入动画。
- (void)keyboardWillShow:(NSNotification *)notif { if (self.hidden == YES) { return; } CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat y = rect.origin.y; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.25]; NSArray *subviews = [self subviews]; for (UIView *sub in subviews) { CGFloat maxY = CGRectGetMaxY(sub.frame); if (maxY > y - 2) { sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2); } } [UIView commitAnimations]; } - (void)keyboardShow:(NSNotification *)notif { if (self.hidden == YES) { return; } } - (void)keyboardWillHide:(NSNotification *)notif { if (self.hidden == YES) { return; } [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.25]; NSArray *subviews = [self subviews]; for (UIView *sub in subviews) { if (sub.center.y < CGRectGetHeight(self.frame)/2.0) { sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0); } } [UIView commitAnimations]; } - (void)keyboardHide:(NSNotification *)notif { if (self.hidden == YES) { return; } }
-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardDidHideNotification object:nil];
- 在键盘将要出现和隐藏的回调中,加入动画。
- (void)keyboardWillShow:(NSNotification *)notif { if (self.hidden == YES) { return; } CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat y = rect.origin.y; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.25]; NSArray *subviews = [self subviews]; for (UIView *sub in subviews) { CGFloat maxY = CGRectGetMaxY(sub.frame); if (maxY > y - 2) { sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2); } } [UIView commitAnimations]; } - (void)keyboardShow:(NSNotification *)notif { if (self.hidden == YES) { return; } } - (void)keyboardWillHide:(NSNotification *)notif { if (self.hidden == YES) { return; } [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.25]; NSArray *subviews = [self subviews]; for (UIView *sub in subviews) { if (sub.center.y < CGRectGetHeight(self.frame)/2.0) { sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0); } } [UIView commitAnimations]; } - (void)keyboardHide:(NSNotification *)notif { if (self.hidden == YES) { return; } }