功能原始碼(扇形進度)及Delegate運用在開放事件中,扇形delegate

來源:互聯網
上載者:User

功能原始碼(扇形進度)及Delegate運用在開放事件中,扇形delegate

1:扇形進度視圖及運用

首先先建立扇形的視圖,傳入進度值

#import <UIKit/UIKit.h>@interface LHProgressView : UIView@property (nonatomic) float progress;@end
#import "LHProgressView.h"#define MinProgress (1.0 / 16.0)@implementation LHProgressView- (id)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        self.backgroundColor = [UIColor clearColor];        _progress = MinProgress;    }    return self;}- (void)drawRect:(CGRect)rect{        CGContextRef context = UIGraphicsGetCurrentContext();        CGContextFillPath(context);    CGRect aRect= CGRectMake(2, 2, self.bounds.size.width - 4, self.bounds.size.height - 4);    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 0.9);    CGContextSetLineWidth(context, 2.0);    CGContextAddEllipseInRect(context, aRect);    CGContextDrawPath(context, kCGPathStroke);        CGFloat centerX = self.bounds.size.width / 2;    CGFloat centerY = self.bounds.size.height / 2;        UIColor *aColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.9];    CGContextSetFillColorWithColor(context, aColor.CGColor);    CGContextSetLineWidth(context, 0.0);    CGContextMoveToPoint(context, centerX, centerY);    CGContextAddArc(context, centerX, centerY, (self.bounds.size.width - 10) / 2,  - M_PI_2, - M_PI_2 + self.progress * 2 *M_PI, 0);    CGContextClosePath(context);    CGContextDrawPath(context, kCGPathFillStroke);}- (void)setProgress:(float)progress{    _progress = progress;        if (_progress < MinProgress) {        _progress = MinProgress;    }        if (_progress >= 1.0) {                [self setNeedsDisplay];        [self removeFromSuperview];            } else {                [self setNeedsDisplay];            }    }@end

運用:

@property(nonatomic, strong)LHProgressView *progressView;
-(instancetype)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {                self.backgroundColor = [UIColor clearColor];                _progressView = [[LHProgressView alloc] init];            }        return self;}
- (void)setItemImageUrl:(NSString *)itemImageUrl{    _itemImageUrl = itemImageUrl;        BOOL imageExist = [[SDWebImageManager sharedManager] cachedImageExistsForURL:[NSURL URLWithString:itemImageUrl]];        if (_itemImageProgress == 1.0 || imageExist) {                [_progressView removeFromSuperview];            } else {                _progressView.bounds = CGRectMake(0, 0, 50, 50);        _progressView.center = CGPointMake((self.bounds.size.width) / 2, (self.bounds.size.height) / 2);        [self addSubview:_progressView];                _progressView.progress = _itemImageProgress;            }        _itemImageView.image = _itemImage;        [self resetSize];        __weak LHProgressView *progressView = _progressView;    __weak LHPhotoView *photoView = self;    NSInteger index = self.tag - 1;        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:itemImageUrl] options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {                if ([photoView.photoViewDelegate respondsToSelector:@selector(photoIsShowingPhotoViewAtIndex:)]) {            BOOL isShow = [photoView.photoViewDelegate photoIsShowingPhotoViewAtIndex:index];                        if (isShow) {                if (receivedSize > kMinProgress) {                    progressView.progress = (float)receivedSize/expectedSize;                }            }                    }                if ([photoView.photoViewDelegate respondsToSelector:@selector(updatePhotoProgress:andIndex:)]) {            [photoView.photoViewDelegate updatePhotoProgress:(float)receivedSize/expectedSize andIndex:index];        }            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {                if (image) {            if ([photoView.photoViewDelegate respondsToSelector:@selector(photoIsShowingPhotoViewAtIndex:)]) {                BOOL isShow = [photoView.photoViewDelegate photoIsShowingPhotoViewAtIndex:index];                                if (isShow) {                    photoView.itemImageView.image = image;                                        [self resetSize];                }                            }                        if ([photoView.photoViewDelegate respondsToSelector:@selector(updatePhotoProgress:andIndex:)]) {                [photoView.photoViewDelegate updatePhotoProgress:1.0 andIndex:index];            }        }                    }];    }

注意:在break裡面要先處理一下對象__weak LHProgressView *progressView = _progressView;上面也用到SDWebImage進行圖片載入,並把進度賦值

 

2:Delegate運用在開放事件中

#import <UIKit/UIKit.h>@class DMDropDownMenu;@protocol DMDropDownMenuDelegate <NSObject>- (void)selectIndex:(NSInteger)index AtDMDropDownMenu:(DMDropDownMenu *)dmDropDownMenu;@end
@interface DMDropDownMenu : UIView@property(nonatomic,assign)id<DMDropDownMenuDelegate>delegate;@end
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [self tapAction];    _curText.text = self.listArr[indexPath.row];    if ([_delegate respondsToSelector:@selector(selectIndex:AtDMDropDownMenu:)]) {        [_delegate selectIndex:indexPath.row AtDMDropDownMenu:self];    }}

運用時三步代碼:

@interface ViewController ()<DMDropDownMenuDelegate>@end    DMDropDownMenu * dm1 = [[DMDropDownMenu alloc] initWithFrame:CGRectMake(10, 150, 299, 30)];    dm1.delegate = self;    [self.view addSubview:dm1];- (void)selectIndex:(NSInteger)index AtDMDropDownMenu:(DMDropDownMenu *)dmDropDownMenu{    NSLog(@"dropDownMenu:%@ index:%d",dmDropDownMenu,index);}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.