幾個常常須要自定義的View總結

幾個常常須要自定義的組件:UIScrollview、UItextView、UIButton

分類: Iphone應用開發 2011-12-28 15:13 136人閱讀 評論(0) 收藏 舉報ide

爲了獨立出組件的一些功能,如,爲UIbutton切換背景圖片,咱們常常須要自定義一些組件,下面是我常常用到的,先總結出來,之後會慢慢更新:函數

-:UIScroviewpost

srollview的事件常常與其子view事件衝突,截斷子view事件的相應spa

//傳遞touch事件.net

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)eventorm

{server

   if(!self.dragging)blog

     

  {事件

        [[selfnextResponder]touchesBegan:toucheswithEvent:event];圖片

  }

   

  [supertouchesBegan:touches withEvent:event];

   

  // NSLog(@"MyScrollView touch Began");

}

 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

   if(!self.dragging)

  {

        [[selfnextResponder]touchesMoved:toucheswithEvent:event];

  }

  [supertouchesMoved:touches withEvent:event];

}

 

 

 

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event

{

   if(!self.dragging)

  {

        [[selfnextResponder]touchesEnded:toucheswithEvent:event];

  }

  [supertouchesEnded:touches withEvent:event];

}

 

[plain] view plaincopy

  1.   



 

//父視圖是否能夠將消息傳遞給子視圖,yes是將事件傳遞給子視圖,則不滾動,no是不傳遞則繼續滾動

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

{

  if ([view isKindOfClass:[CustomUITextViewclass]])

  {

     return YES;

  }

  else 

  {

    returnNO;

   

  }

 

}

 

//Yes是子視圖取消繼續接受touch消息(能夠滾動),NO是子視圖能夠繼續接受touch事件(不滾動)

//默認的狀況下當view不是一個UIControlo類的時候,值是yes,不然是no 

//調用狀況是這樣的通常是在發送tracking messages消息後會調用這個函數,來判斷scroll是否滾動,仍是接受子視圖的touch事件

- (BOOL)touchesShouldCancelInContentView:(UIView *)view

{

  NSLog(@"用戶點擊的視圖 %@",view);

   returnNO;


二:UITextView默認是沒有邊框的,能夠給它加個凹下去的邊框

-(void) drawRect:(CGRect)rect {

   

    [self.layersetBackgroundColor: [[UIColorwhiteColor]CGColor]];

    [self.layersetBorderColor: [[UIColorgrayColor]CGColor]];

    [self.layersetBorderWidth:1.0];

    [self.layersetCornerRadius:8.0f];

    [self.layersetMasksToBounds:YES];

   UIGraphicsBeginImageContext(self.frame.size);

   CGContextRef currentContext =UIGraphicsGetCurrentContext();

  CGContextSetLineWidth(currentContext, 2.0);

  CGContextSetRGBStrokeColor(currentContext, 0.6,0.6,.61.0);

  CGRect myRect = CGContextGetClipBoundingBox(currentContext);  

  float myShadowColorValues[] = {0,0,0,1};

   CGColorSpaceRef myColorSpace =CGColorSpaceCreateDeviceRGB();

  CGColorRef colorRef = CGColorCreate(myColorSpace, myShadowColorValues);

  CGContextSetShadowWithColor(currentContext, CGSizeMake(-1,1),2, colorRef);

   

  CGContextStrokeRect(currentContext, myRect);

   UIImage *backgroundImage = (UIImage *)UIGraphicsGetImageFromCurrentImageContext();

   UIImageView *myImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];

  [myImageView setImage:backgroundImage];

  [selfaddSubview:myImageView];

  [myImageView release];

   UIGraphicsEndImageContext();

}


三:咱們會想按下按鈕時,切換button的圖片背景,能夠給UIbutton加個UIControllEvent事件的消息通知,當按鈕被按下的時候,通知按鈕全部者去切換圖片

- (id)initWithFrame:(CGRect)_frame  {

if (self = [superinitWithFrame:_frame]) {

[selfaddTarget:selfaction:@selector(touchDown:)forControlEvents:UIControlEventTouchDown];

[selfaddTarget:selfaction:@selector(touchUpInside:)forControlEvents:UIControlEventTouchUpInside];

//[selfaddTarget:selfaction:@selector(touchUpOutside:)forControlEvents:UIControlEventTouchUpOutside];

}

 

returnself;

}

 

- (void)touchDown:(id)sender {

NSNotification *notification = [NSNotificationnotificationWithName:@"TouchDownButton"object:selfuserInfo:nil];

[[NSNotificationCenterdefaultCenter]postNotification:notification];

NSLog(@"%s",__FUNCTION__);

}

 

- (void)touchUpInside:(id)sender {

//[self setBackgroundImage:@"next.png" forState:UIControlStateNormal];

NSNotification *notification = [NSNotificationnotificationWithName:@"TouchUpButton"object:selfuserInfo:nil];

[[NSNotificationCenterdefaultCenter]postNotification:notification];

 

}

使用方法

在全部者類中定義這些自定義的組件,如定義

CustomerButton *nextButton;

監聽消息

[notification addObserver:self selector:@selector(touchDownNext) name:@"TouchDownButton" object:nil];

[notification addObserver:self selector:@selector(touchUpNext) name:@"TouchUpButton" object:nil];

監聽到後須要執行的動做

-(void)touchDownNext{

UIImage *image = [UIImageimageNamed:@"next_pressed.png"];

[nextButtonsetBackgroundImage:imageforState:UIControlStateHighlighted];

}

 

-(void)touchUpNext{

UIImage *image = [UIImageimageNamed:@"next.png"];

[nextButtonsetBackgroundImage:imageforState:UIControlStateNormal];

}

相關文章
相關標籤/搜索