HGPhoto.m 8.03 KB
Newer Older
freecui's avatar
freecui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
//
//  Photo.m
//  PhotoDemo
//
//  Created by Harry on 12-12-6.
//  Copyright (c) 2012年 Harry. All rights reserved.
//

#import "HGPhoto.h"

#import <QuartzCore/QuartzCore.h>
#import "UIImageView+WebCache.h"
static CGRect oldframe;
@interface HGPhoto() <UIGestureRecognizerDelegate>

@property (strong, nonatomic) UIView *viewMask;
@property (strong, nonatomic) UIImageView *viewPhoto;
@property (strong, nonatomic) UIButton *delBtn;

@property (strong, nonatomic) NSString *stringImageUrl;
@property (nonatomic) CGPoint pointOrigin;
@property (nonatomic) BOOL editModel;

@property (nonatomic) PhotoType type;


@end

#define kPhotoSize 100. //75//

@implementation HGPhoto


- (id)initWithOrigin:(CGPoint)origin
{
    self = [super initWithFrame:CGRectMake(origin.x, origin.y, kPhotoSize, kPhotoSize)];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        self.viewPhoto = [[UIImageView alloc] initWithFrame:self.bounds];
        self.viewPhoto.layer.cornerRadius = 12;
        self.viewPhoto.layer.masksToBounds = YES;
        self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_delBtn setBackgroundImage:[UIImage imageNamed:@"attachment_delete_btn"] forState:UIControlStateNormal];
        CGFloat btnX = _viewPhoto.frame.size.width * 0.7;
        CGFloat btnY = 0;
        CGFloat btnW = _viewPhoto.frame.size.width - btnX;
        _delBtn.frame = CGRectMake(btnX, btnY, btnW, btnW);
        [_delBtn addTarget:self action:@selector(delphoto) forControlEvents:UIControlEventTouchUpInside];
        [_viewPhoto addSubview:_delBtn];
       
        
   
        
        self.viewMask = [[UIView alloc] initWithFrame:self.bounds];
        self.viewMask.alpha = 0.6;
        self.viewMask.backgroundColor = [UIColor blackColor];
        self.viewMask.layer.cornerRadius = 11;
        self.viewMask.layer.masksToBounds = YES;
        
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
        
        [self addSubview:self.viewPhoto];
        [self addSubview:self.viewMask];
        [self addGestureRecognizer:tapRecognizer];
        
        self.editModel = NO;
        self.viewMask.hidden = YES;
    }
    self.autoresizingMask = UIViewContentModeScaleAspectFill;
    //self.autoresizingMask = UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleHeight;
    return self;
}
- (id)initWithOrigin:(CGPoint)origin andLength:(CGFloat)length {
    self =  [self initWithOrigin:origin];
    self.viewPhoto.size = CGSizeMake(length, length);
    self.size = CGSizeMake(length, length);
    return self;
    
}
- (void)setPhotoType:(PhotoType)type
{
    self.type = type;
    if (type == PhotoTypeAdd) {
        self.viewPhoto.image = [UIImage imageNamed:@"AttachCamera"];//@"addPhoto"];
         [_delBtn removeFromSuperview];
    }
}

- (PhotoType)getPhotoType
{
    return self.type;
}

- (void)setPhotoUrl:(NSString*)photoUrl
{
    [self.viewPhoto setImageWithURL:[NSURL URLWithString:photoUrl] placeholderImage:nil];
}
- (void)setPhotoImg:(UIImage *)img {
    [self.viewPhoto setImage:img];
}
- (void)moveToPosition:(CGPoint)point
{
    if (self.type == PhotoTypePhoto) {
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = CGRectMake(point.x, point.y, self.frame.size.width, self.frame.size.height);
        } completion:nil];
    } else {
        self.frame = CGRectMake(point.x, point.y, self.frame.size.width, self.frame.size.height);
    }
}

- (void)setEditModel:(BOOL)edit
{
    if (self.type == PhotoTypePhoto) {
        if (edit) {
            UILongPressGestureRecognizer *longPressreRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
            longPressreRecognizer.delegate = self;
            [self addGestureRecognizer:longPressreRecognizer];
        } else {
            for (UIGestureRecognizer *recognizer in [self gestureRecognizers]) {
                if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
                    [self removeGestureRecognizer:recognizer];
                    break;
                }
            }
        }
    }
}
#pragma btn action
- (void)delphoto {
    if ([self.delegate respondsToSelector:@selector(photoDel:)]) {
        [self.delegate photoDel:self];
    }
}

#pragma mark - UITapGestureRecognizer

- (void)tapPress: (UITapGestureRecognizer *)tap//(id)sender
{
    //根据点击的位置不同去响应事件
    CGMutablePathRef pathRef=CGPathCreateMutable();
    CGPathMoveToPoint(pathRef, NULL, _delBtn.frame.origin.x, _delBtn.frame.origin.y);
    CGPathAddLineToPoint(pathRef, NULL,  _delBtn.frame.origin.x, _delBtn.frame.origin.y);
    CGPathAddLineToPoint(pathRef, NULL, _delBtn.frame.origin.x + _delBtn.frame.size.width, _delBtn.frame.origin.y);
    CGPathAddLineToPoint(pathRef, NULL, _delBtn.frame.origin.x,_delBtn.frame.origin.y + _delBtn.frame.size.height );

    CGPathAddLineToPoint(pathRef, NULL, _delBtn.frame.origin.x + _delBtn.frame.size.width, _delBtn.frame.origin.y + _delBtn.frame.size.height);
    CGPathCloseSubpath(pathRef);
    
    CGPoint p = [tap locationInView:self];
    
    if (CGPathContainsPoint(pathRef, NULL, p, NO)) {
        NSLog(@"point in path!");
        if ([self.delegate respondsToSelector:@selector(photoDel:)]) {
            [self.delegate photoDel:self];
        }
    }
    else {
        NSLog(@"outPoint out path!");
        if ([self.delegate respondsToSelector:@selector(photoTaped:)]) {
        [self.delegate photoTaped:self];
        }
    }
}

- (void)handleLongPress:(id)sender
{
    UILongPressGestureRecognizer *recognizer = sender;
    CGPoint point = [recognizer locationInView:self];
    
    CGFloat diffx = 0.;
    CGFloat diffy = 0.;
    
    if (UIGestureRecognizerStateBegan == recognizer.state) {
        self.viewMask.hidden = NO;
        self.pointOrigin = point;
        [self.superview bringSubviewToFront:self];
    } else if (UIGestureRecognizerStateEnded == recognizer.state) {
        self.viewMask.hidden = YES;
        if ([self.delegate respondsToSelector:@selector(photoMoveFinished:)]) {
            [self.delegate photoMoveFinished:self];
        }
    } else {
        diffx = point.x - self.pointOrigin.x;
        diffy = point.y - self.pointOrigin.y;
    }
    
    CGFloat originx = self.frame.origin.x +diffx;
    CGFloat originy = self.frame.origin.y +diffy;
    
    self.frame = CGRectMake(originx, originy, self.frame.size.width, self.frame.size.height);
}


- (void)showImage {
    UIImage *image = _viewPhoto.image;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *backgroundView = [[UIView alloc]init];
    backgroundView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    oldframe = [_viewPhoto convertRect:_viewPhoto.bounds toView:window ];
    backgroundView.backgroundColor=[UIColor blackColor];
    backgroundView.alpha = 0;
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = image;
    imageView.tag = 1;
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer:tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = CGRectMake(
                    0,
    ([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2,
    [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
        backgroundView.alpha = 1;

    } completion:^(BOOL finished) {
        
    }];
    
}

- (void)hideImage : (UITapGestureRecognizer *)tap {
    UIView *backgroundView = tap.view;
    UIImageView *imageView = (UIImageView *)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame = oldframe;
        backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}
@end