日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

几个经常需要自定义的View总结

發布時間:2024/9/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 几个经常需要自定义的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总结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。