几个经常需要自定义的View总结
2019獨角獸企業重金招聘Python工程師標準>>>
幾個經常需要自定義的組件:UIScrollview、UItextView、UIButton
分類:?Iphone應用開發?2011-12-28 15:13?136人閱讀?評論(0)?收藏?舉報
為了獨立出組件的一些功能,如,為UIbutton切換背景圖片,我們經常需要自定義一些組件,下面是我經常用到的,先總結出來,以后會慢慢更新:
-:UIScroview
srollview的事件經常與其子view事件沖突,截斷子view事件的相應
//傳遞touch事件
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
???if(!self.dragging)
?????
??{
? ? ? ? [[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
??
?
//父視圖是否可以將消息傳遞給子視圖,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,.6,?1.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];
}
轉載于:https://my.oschina.net/u/2473136/blog/516177
總結
以上是生活随笔為你收集整理的几个经常需要自定义的View总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS 委托和协议区别和联系 (-)
- 下一篇: 快速开发平台网格部件合并单元格。