From 192e79d91dda587c3e79d371c96f141b112dddb6 Mon Sep 17 00:00:00 2001 From: Alexandru Macarescu Date: Wed, 29 Oct 2014 11:50:37 +0200 Subject: [PATCH] Support for half values --- DYRateView.xcodeproj/project.pbxproj | 18 ++++++++++++ DYRateView/DYRateView/DYRateView.h | 7 +++-- DYRateView/DYRateView/DYRateView.m | 33 ++++++++++++++++------ DYRateView/DemoViewController.m | 4 +-- DYRateView/Resources/StarHalf.png | Bin 0 -> 1256 bytes DYRateView/Resources/StarHalf@2x.png | Bin 0 -> 1374 bytes DYRateView/Resources/StarHalfLarge.png | Bin 0 -> 1457 bytes DYRateView/Resources/StarHalfLarge@2x.png | Bin 0 -> 1567 bytes 8 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 DYRateView/Resources/StarHalf.png create mode 100644 DYRateView/Resources/StarHalf@2x.png create mode 100644 DYRateView/Resources/StarHalfLarge.png create mode 100644 DYRateView/Resources/StarHalfLarge@2x.png diff --git a/DYRateView.xcodeproj/project.pbxproj b/DYRateView.xcodeproj/project.pbxproj index c16df98..acef996 100644 --- a/DYRateView.xcodeproj/project.pbxproj +++ b/DYRateView.xcodeproj/project.pbxproj @@ -7,6 +7,10 @@ objects = { /* Begin PBXBuildFile section */ + 0886168C1A00ECC200E0F0EE /* StarHalf.png in Resources */ = {isa = PBXBuildFile; fileRef = 088616881A00ECC200E0F0EE /* StarHalf.png */; }; + 0886168D1A00ECC200E0F0EE /* StarHalf@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 088616891A00ECC200E0F0EE /* StarHalf@2x.png */; }; + 0886168E1A00ECC200E0F0EE /* StarHalfLarge.png in Resources */ = {isa = PBXBuildFile; fileRef = 0886168A1A00ECC200E0F0EE /* StarHalfLarge.png */; }; + 0886168F1A00ECC200E0F0EE /* StarHalfLarge@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 0886168B1A00ECC200E0F0EE /* StarHalfLarge@2x.png */; }; D353DC55F2240468AE06EA5E /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D353DC55F2240468AE06EA5D /* UIKit.framework */; }; D353DC55F2240468AE06EA60 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D353DC55F2240468AE06EA5F /* Foundation.framework */; }; D353DC55F2240468AE06EA62 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D353DC55F2240468AE06EA61 /* CoreGraphics.framework */; }; @@ -27,6 +31,10 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 088616881A00ECC200E0F0EE /* StarHalf.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = StarHalf.png; sourceTree = ""; }; + 088616891A00ECC200E0F0EE /* StarHalf@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "StarHalf@2x.png"; sourceTree = ""; }; + 0886168A1A00ECC200E0F0EE /* StarHalfLarge.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = StarHalfLarge.png; sourceTree = ""; }; + 0886168B1A00ECC200E0F0EE /* StarHalfLarge@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "StarHalfLarge@2x.png"; sourceTree = ""; }; D353DC55F2240468AE06EA54 /* DYRateView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DYRateView.app; sourceTree = BUILT_PRODUCTS_DIR; }; D353DC55F2240468AE06EA5D /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; D353DC55F2240468AE06EA5F /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -129,6 +137,10 @@ D353DC55F2240468AE06EA79 /* Resources */ = { isa = PBXGroup; children = ( + 088616881A00ECC200E0F0EE /* StarHalf.png */, + 088616891A00ECC200E0F0EE /* StarHalf@2x.png */, + 0886168A1A00ECC200E0F0EE /* StarHalfLarge.png */, + 0886168B1A00ECC200E0F0EE /* StarHalfLarge@2x.png */, D353DC55F2240468AE06EA88 /* StarEmpty.png */, D353DC55F2240468AE06EA86 /* StarEmpty@2x.png */, D353DC55F2240468AE06EA84 /* StarEmptyLarge.png */, @@ -166,6 +178,8 @@ /* Begin PBXProject section */ D353DC55F2240468AE06EA4E /* Project object */ = { isa = PBXProject; + attributes = { + }; buildConfigurationList = D353DC55F2240468AE06EA4F /* Build configuration list for PBXProject "DYRateView" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; @@ -189,15 +203,19 @@ buildActionMask = 2147483647; files = ( D353DC55F2240468AE06EA68 /* InfoPlist.strings in Resources */, + 0886168D1A00ECC200E0F0EE /* StarHalf@2x.png in Resources */, D353DC55F2240468AE06EA74 /* (null) in Resources */, D353DC55F2240468AE06EA7B /* StarFullLarge@2x.png in Resources */, D353DC55F2240468AE06EA7D /* StarFullLarge.png in Resources */, D353DC55F2240468AE06EA7F /* StarFull@2x.png in Resources */, + 0886168C1A00ECC200E0F0EE /* StarHalf.png in Resources */, D353DC55F2240468AE06EA81 /* StarFull.png in Resources */, D353DC55F2240468AE06EA83 /* StarEmptyLarge@2x.png in Resources */, D353DC55F2240468AE06EA85 /* StarEmptyLarge.png in Resources */, D353DC55F2240468AE06EA87 /* StarEmpty@2x.png in Resources */, D353DC55F2240468AE06EA89 /* StarEmpty.png in Resources */, + 0886168E1A00ECC200E0F0EE /* StarHalfLarge.png in Resources */, + 0886168F1A00ECC200E0F0EE /* StarHalfLarge@2x.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/DYRateView/DYRateView/DYRateView.h b/DYRateView/DYRateView/DYRateView.h index 175a5e4..38a24db 100644 --- a/DYRateView/DYRateView/DYRateView.h +++ b/DYRateView/DYRateView/DYRateView.h @@ -32,12 +32,12 @@ typedef enum { RateViewAlignmentRight } RateViewAlignment; - @interface DYRateView : UIView { UIImage *_fullStarImage; UIImage *_emptyStarImage; + UIImage *_halfStarImage; CGPoint _origin; - NSInteger _numOfStars; + CGFloat _numOfStars; } @property(nonatomic, assign) RateViewAlignment alignment; @@ -46,10 +46,11 @@ typedef enum { @property(nonatomic, assign) BOOL editable; @property(nonatomic, retain) UIImage *fullStarImage; @property(nonatomic, retain) UIImage *emptyStarImage; +@property(nonatomic, retain) UIImage *halfStarImage; @property(nonatomic, assign) NSObject *delegate; - (DYRateView *)initWithFrame:(CGRect)frame; -- (DYRateView *)initWithFrame:(CGRect)rect fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage; +- (DYRateView *)initWithFrame:(CGRect)rect fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage halfStar:(UIImage *)halfStarImage; @end diff --git a/DYRateView/DYRateView/DYRateView.m b/DYRateView/DYRateView/DYRateView.m index b4aeef5..46d32bd 100644 --- a/DYRateView/DYRateView/DYRateView.m +++ b/DYRateView/DYRateView/DYRateView.m @@ -29,6 +29,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE static NSString *DefaultFullStarImageFilename = @"StarFull.png"; static NSString *DefaultEmptyStarImageFilename = @"StarEmpty.png"; +static NSString *DefaultHalfStarImageFilename = @"StarHalf.png"; @interface DYRateView () @@ -46,13 +47,14 @@ @implementation DYRateView @synthesize editable = _editable; @synthesize fullStarImage = _fullStarImage; @synthesize emptyStarImage = _emptyStarImage; +@synthesize halfStarImage = _halfStarImage; @synthesize delegate = _delegate; - (DYRateView *)initWithFrame:(CGRect)frame { - return [self initWithFrame:frame fullStar:[UIImage imageNamed:DefaultFullStarImageFilename] emptyStar:[UIImage imageNamed:DefaultEmptyStarImageFilename]]; + return [self initWithFrame:frame fullStar:[UIImage imageNamed:DefaultFullStarImageFilename] emptyStar:[UIImage imageNamed:DefaultEmptyStarImageFilename] halfStar:[UIImage imageNamed:DefaultHalfStarImageFilename]]; } -- (DYRateView *)initWithFrame:(CGRect)frame fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage { +- (DYRateView *)initWithFrame:(CGRect)frame fullStar:(UIImage *)fullStarImage emptyStar:(UIImage *)emptyStarImage halfStar:(UIImage *)halfStarImage { self = [super initWithFrame:frame]; if (self) { self.opaque = NO; @@ -60,6 +62,7 @@ - (DYRateView *)initWithFrame:(CGRect)frame fullStar:(UIImage *)fullStarImage em _fullStarImage = [fullStarImage retain]; _emptyStarImage = [emptyStarImage retain]; + _halfStarImage = [halfStarImage retain]; [self commonSetup]; } @@ -71,6 +74,7 @@ - (id)initWithCoder:(NSCoder *)decoder { if (self) { _fullStarImage = [[UIImage imageNamed:DefaultFullStarImageFilename] retain]; _emptyStarImage = [[UIImage imageNamed:DefaultEmptyStarImageFilename] retain]; + _halfStarImage = [[UIImage imageNamed:DefaultHalfStarImageFilename] retain]; [self commonSetup]; } @@ -80,6 +84,7 @@ - (id)initWithCoder:(NSCoder *)decoder { - (void)dealloc { [_fullStarImage release]; _fullStarImage = nil; [_emptyStarImage release]; _emptyStarImage = nil; + [_halfStarImage release]; _halfStarImage = nil; [super dealloc]; } @@ -119,12 +124,16 @@ - (void)drawRect:(CGRect)rect x += _fullStarImage.size.width + _padding; } - float floor = floorf(_rate); x = _origin.x; for (int i = 0; i < floor; i++) { - [_fullStarImage drawAtPoint:CGPointMake(x, _origin.y)]; - x += _fullStarImage.size.width + _padding; + if (_rate - (CGFloat)i == 0.5) { + [_halfStarImage drawAtPoint:CGPointMake(x, _origin.y)]; + x += _halfStarImage.size.width + _padding; + } else { + [_fullStarImage drawAtPoint:CGPointMake(x, _origin.y)]; + x += _fullStarImage.size.width + _padding; + } } if (_numOfStars - floor > 0.01) { @@ -168,10 +177,18 @@ - (void)setEmptyStarImage:(UIImage *)emptyStarImage } } +- (void)setHalfStarImage:(UIImage *)halfStarImage { + if (halfStarImage != _halfStarImage) { + [_halfStarImage release]; + _halfStarImage = [halfStarImage retain]; + [self setNeedsDisplay]; + } +} + - (void)handleTouchAtLocation:(CGPoint)location { - for(int i = _numOfStars - 1; i > -1; i--) { - if (location.x > _origin.x + i * (_fullStarImage.size.width + _padding) - _padding / 2.) { - self.rate = i + 1; + for(float i = _numOfStars*2 - 1; i > -1; i--) { + if (location.x > _origin.x + i/2 * (_fullStarImage.size.width + _padding) - _padding / 2.) { + self.rate = i/2.0 + 0.5; return; } } diff --git a/DYRateView/DemoViewController.m b/DYRateView/DemoViewController.m index 47dd990..625e792 100644 --- a/DYRateView/DemoViewController.m +++ b/DYRateView/DemoViewController.m @@ -53,7 +53,7 @@ - (void)viewDidLoad } - (void)setUpEditableRateView { - DYRateView *rateView = [[DYRateView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:@"StarFullLarge.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge.png"]]; + DYRateView *rateView = [[DYRateView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:@"StarFullLarge.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge.png"] halfStar:[UIImage imageNamed:@"StarHalfLarge.png"]]; rateView.padding = 20; rateView.alignment = RateViewAlignmentCenter; rateView.editable = YES; @@ -113,7 +113,7 @@ - (void)dealloc { #pragma mark - DYRateViewDelegate - (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate { - self.rateLabel.text = [NSString stringWithFormat:@"Rate: %d", rate.intValue]; + self.rateLabel.text = [NSString stringWithFormat:@"Rate: %.1f", rate.floatValue]; } @end \ No newline at end of file diff --git a/DYRateView/Resources/StarHalf.png b/DYRateView/Resources/StarHalf.png new file mode 100644 index 0000000000000000000000000000000000000000..b70dffc4b95279ce05993011b4d180a80220b56c GIT binary patch literal 1256 zcmeAS@N?(olHy`uVBq!ia0vp^d?3uh1|;P@bT0xa$r9IylHmNblJdl&R0hYC{G?O` z&)mfH)S%SFl*+=BsWuD@%qp275hW46K32*3xq68pHF_1f1wh>l3^w)^1&PVosU-?Y zsp*+{wo31J?^jaDOtDo8H}y5}EpSfF$n>ZxN)4{^3rViZPPR-@vbR&PsjvbXkegbP zs8ErclUHn2VXFi-*9yo63F|81#=KlDb#X~hD#E>34K5C;EJ)Q4 zN-fSWElLJPT$(b-ssbzLqSVBa{GyQj{2W*)24v)y-fM)1rW~Nw~n3y?P8kkv_nwuLN8oHWTI++_=xVX6hDMup{ zHz$~0m;B_?+|;}hnBEkGUQ3*ML5U%^0BEyIYEfocYKmJ?ey#%8(^i?d-C~B*JgD9j z+-@<)saGH97=2LGB1JV!2$+6AOnAZta^OinH4m8Hi+~BcvU=$%1_s6`PZ!6Kid%Cg z?e=1J6lwcEbMD<7A^pY$NxDs;QrR0j5)>m|GG`=aF6Mv8eB&pR*}j$tu0_6D$;o<3 zofBiq%JtIc_#QbYC?hc=@!9OpbB)_0vp>#1IB)&6iJN;EKZo)O8VNY=*t}uaeUCck zpHs>vs47TCWw1)LhU#Ws7N7XPrA&LK&?QC78+ihqyTnT_Dsgi^{&T{z|2S8qSM||~ z#MA6t$psSb7b_*YwwYbId`so2Q%w-pB5VB@Th^(cx-wnt(c-D6=A14Slbq?dca`IT z-b=rJEd9FiZsvD(orwu6n|!Byx3&n2OC P1(iLXu6{1-oD!M<4gtBD literal 0 HcmV?d00001 diff --git a/DYRateView/Resources/StarHalf@2x.png b/DYRateView/Resources/StarHalf@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..5be6f5c1eec241bcd8553f2be6a09c4961943e59 GIT binary patch literal 1374 zcmeAS@N?(olHy`uVBq!ia0vp^G9b*s1|*Ak?@s|zk|nMYCBgY=CFO}lsSJ)O`AMk? zp1FzXsX?iUDV2pMQ*9U+m{l@EB1$5BeXNr6bM+EIYV;~{3xK*A7;Nk-3KEmEQ%e+* zQqwc@Y?a>c-mj#PnPRIHZt82`Ti~3Uk?B!Ylp0*+7m{3+ootz+WN)WnQ(*-(AUCxn zQK2F?C$HG5!d3}vt`(3C64qBz04piUwpD^SD#ABF!8yMuRl!uxSU1_g&``n5OwZ87 z)XdCKN5ROz&`93^h|F{iO{`4Ktc=VRpg;*|TTx1yRgjAt)Gi>;Rw<*Tq`*pFzr4I$ zuiRKKzbIYb(9+TpWQLKEE>MMTab;dfVufyAu`f(~1RD^r68eAMwS&*t9 zlvO-#!!vxW&l8$k^1( z%?+n{P`xQw+yd5XZeV~@uRhQ*`k<&qifWh;F#Uj-@PrHGz>|Jz9x%NZ0TVW#|9Kw< z2F44XE{-7;xAsii?RVHfq|JYhNp6++D~ED6F*UZ92?892!a~zo*#x9}I6iXed|>d= z(dlTFoY&OYddNXgT$97=){>>Zw~jnxQE`ZwZMAZ#zuzO}zdv*5Cvz`-ocG~Ykg#RO zR?i-%=-Do}b#F8sHVMiKoe;o&KzXwbU*LpBcekI{E_@V`cVxS*X(1H0NYl^pnQEPt zj<(VC1()|YUo6$|R+HCn|vhR^J;g1p10t9}@+$!k$8>b}p_OEe(h-XaTp@iTE zcTFna94}eCYif^M*knUjzIK(5$1_gdn>10KUv@TQlG1C}#oLP3X0gVuzZO6D4_o-7 z+dnSLzfpF66|DMwMd%s_mb1rFVtV+_8=n)3@qOyg|9f3tF5vj2^x7I{R!KjB#vf`w{4-~t z)KKxWC_eA~ah^~2hj-_A_pS_BuD#&#$+c#WGbaj$ugox9cJ07Loo%vH9ymm`^;Eyy zsm*+L`ofO;(|pY36LB0Guy u(!#J-*EjsP3I)qpe@`^tuYX9NiHG4ZgU6?T&YRspC7!3NpUXO@geCwo0PZOO literal 0 HcmV?d00001 diff --git a/DYRateView/Resources/StarHalfLarge.png b/DYRateView/Resources/StarHalfLarge.png new file mode 100644 index 0000000000000000000000000000000000000000..874cf6d8c1ac58853a3682753185b6b8cac78e4a GIT binary patch literal 1457 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc1|)ksWqE;=WQl7;NpOBzNqJ&XDuZK6ep0G} zXKrG8YEWuoN@d~6R2v2cW|hp4h>{3jAFJg2T)jk)8oi3#0-$aN1{?c|g2d$P)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