-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new SurveyViewCell with AnswerCollectionView and custom AnswerCell
- Loading branch information
Casing Chu
committed
Feb 21, 2015
1 parent
78b90a4
commit c02c201
Showing
13 changed files
with
467 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// AnswerCollectionView.h | ||
// unnamed | ||
// | ||
// Created by Casing Chu on 2/20/15. | ||
// Copyright (c) 2015 com.yahoo. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
@interface AnswerCollectionView : UIView | ||
|
||
@property (nonatomic, strong) NSArray *answers; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// | ||
// AnswerCollectionView.m | ||
// unnamed | ||
// | ||
// Created by Casing Chu on 2/20/15. | ||
// Copyright (c) 2015 com.yahoo. All rights reserved. | ||
// | ||
|
||
#import "AnswerCollectionView.h" | ||
#import "AnswerView.h" | ||
#import "Answer.h" | ||
|
||
@interface AnswerCollectionView () | ||
|
||
@property (nonatomic, assign) NSInteger total; | ||
@property (nonatomic, strong) NSMutableArray *answerViews; | ||
|
||
- (NSInteger)getTotalFromAnswers:(NSArray *)answers; | ||
|
||
@end | ||
|
||
@implementation AnswerCollectionView | ||
|
||
/* | ||
// Only override drawRect: if you perform custom drawing. | ||
// An empty implementation adversely affects performance during animation. | ||
- (void)drawRect:(CGRect)rect { | ||
// Drawing code | ||
} | ||
*/ | ||
|
||
- (void)baseInit { | ||
self.answerViews = [[NSMutableArray alloc] init]; | ||
self.total = 0; | ||
} | ||
|
||
- (id)initWithFrame:(CGRect)frame | ||
{ | ||
self = [super initWithFrame:frame]; | ||
if (self) { | ||
[self baseInit]; | ||
} | ||
return self; | ||
} | ||
|
||
- (id)initWithCoder:(NSCoder *)aDecoder { | ||
if ((self = [super initWithCoder:aDecoder])) { | ||
[self baseInit]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)setAnswers:(NSArray *)answers { | ||
_answers = answers; | ||
_total = [self getTotalFromAnswers:answers]; | ||
|
||
int count = 1; | ||
[self.answerViews removeAllObjects]; | ||
for (Answer *answer in self.answers) { | ||
AnswerView *view = [[AnswerView alloc] init]; | ||
view.index = count; | ||
view.total = self.total; | ||
view.answer = answer; | ||
count++; | ||
[self.answerViews addObject:view]; | ||
[self addSubview:view]; | ||
} | ||
|
||
[self setNeedsLayout]; | ||
} | ||
|
||
- (void)layoutSubviews { | ||
[super layoutSubviews]; | ||
|
||
for (int i=0;i<self.answerViews.count;i++) { | ||
AnswerView *view = self.answerViews[i]; | ||
CGRect imageFrame = CGRectMake(0, view.frame.size.height * i, self.frame.size.width, self.frame.size.height); | ||
view.frame = imageFrame; | ||
} | ||
|
||
} | ||
|
||
#pragma mark - Private Methods | ||
- (NSInteger)getTotalFromAnswers:(NSArray *)answers { | ||
NSInteger total = 0; | ||
for (Answer *ans in answers) { | ||
total += ans.count; | ||
} | ||
return total; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// AnswerView.h | ||
// unnamed | ||
// | ||
// Created by Casing Chu on 2/20/15. | ||
// Copyright (c) 2015 com.yahoo. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "Answer.h" | ||
|
||
@interface AnswerView : UIView | ||
|
||
@property (nonatomic, strong) Answer *answer; | ||
@property (nonatomic, assign) NSInteger total; | ||
@property (nonatomic, assign) NSInteger index; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// AnswerView.m | ||
// unnamed | ||
// | ||
// Created by Casing Chu on 2/20/15. | ||
// Copyright (c) 2015 com.yahoo. All rights reserved. | ||
// | ||
|
||
#import "AnswerView.h" | ||
#import "BarView.h" | ||
|
||
@interface AnswerView () | ||
@property (weak, nonatomic) IBOutlet BarView *barView; | ||
@property (weak, nonatomic) IBOutlet UILabel *percentLabel; | ||
@property (weak, nonatomic) IBOutlet UILabel *answerLabel; | ||
@property (strong, nonatomic) IBOutlet UIView *containerView; | ||
|
||
@end | ||
|
||
@implementation AnswerView | ||
|
||
/* | ||
// Only override drawRect: if you perform custom drawing. | ||
// An empty implementation adversely affects performance during animation. | ||
- (void)drawRect:(CGRect)rect { | ||
// Drawing code | ||
} | ||
*/ | ||
|
||
- (void)baseInit { | ||
UINib *nib = [UINib nibWithNibName:@"AnswerView" bundle:nil]; | ||
[nib instantiateWithOwner:self options:nil]; | ||
|
||
self.containerView.frame = self.bounds; | ||
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | ||
[self addSubview:self.containerView]; | ||
} | ||
|
||
- (id)initWithFrame:(CGRect)frame | ||
{ | ||
self = [super initWithFrame:frame]; | ||
if (self) { | ||
[self baseInit]; | ||
} | ||
return self; | ||
} | ||
|
||
- (id)initWithCoder:(NSCoder *)aDecoder { | ||
if ((self = [super initWithCoder:aDecoder])) { | ||
[self baseInit]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)setAnswer:(Answer *)answer { | ||
_answer = answer; | ||
self.answerLabel.text = [NSString stringWithFormat:@"%ld. %@", self.index, answer.text]; | ||
CGFloat percentage = self.total <= 0 ? 0.0 : 1.0f * answer.count / self.total; | ||
self.percentLabel.text = [NSString stringWithFormat:@"%.1f%%", percentage * 100.0]; | ||
self.barView.percentage = percentage; | ||
|
||
[self setNeedsLayout]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6254" systemVersion="14C109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES"> | ||
<dependencies> | ||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/> | ||
</dependencies> | ||
<objects> | ||
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="AnswerView"> | ||
<connections> | ||
<outlet property="answerLabel" destination="6zp-Vs-ovw" id="uNi-7F-f3E"/> | ||
<outlet property="barView" destination="O7j-Bf-ACJ" id="sjf-gt-xEr"/> | ||
<outlet property="containerView" destination="m8k-6m-S8c" id="yYm-jl-tr7"/> | ||
<outlet property="percentLabel" destination="R6c-5O-Ip8" id="hBo-lB-O6C"/> | ||
</connections> | ||
</placeholder> | ||
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/> | ||
<view contentMode="scaleToFill" id="m8k-6m-S8c"> | ||
<rect key="frame" x="0.0" y="0.0" width="320" height="24"/> | ||
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> | ||
<subviews> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="100.0%" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="R6c-5O-Ip8"> | ||
<rect key="frame" x="51" y="2" width="43.5" height="14.5"/> | ||
<constraints> | ||
<constraint firstAttribute="width" constant="28" id="3rh-hP-pRR"/> | ||
</constraints> | ||
<fontDescription key="fontDescription" type="system" pointSize="12"/> | ||
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/> | ||
<nil key="highlightedColor"/> | ||
<variation key="default"> | ||
<mask key="constraints"> | ||
<exclude reference="3rh-hP-pRR"/> | ||
</mask> | ||
</variation> | ||
</label> | ||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1. The Answer Is" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="6zp-Vs-ovw"> | ||
<rect key="frame" x="98" y="2" width="93" height="14.5"/> | ||
<fontDescription key="fontDescription" type="system" pointSize="12"/> | ||
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/> | ||
<nil key="highlightedColor"/> | ||
</label> | ||
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="O7j-Bf-ACJ" customClass="BarView"> | ||
<rect key="frame" x="8" y="5" width="40" height="8"/> | ||
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/> | ||
<constraints> | ||
<constraint firstAttribute="width" constant="40" id="0l3-oJ-CVJ"/> | ||
<constraint firstAttribute="height" constant="8" id="eTG-9f-CWP"/> | ||
</constraints> | ||
</view> | ||
</subviews> | ||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> | ||
<constraints> | ||
<constraint firstItem="R6c-5O-Ip8" firstAttribute="centerY" secondItem="6zp-Vs-ovw" secondAttribute="centerY" id="AzX-HI-mZd"/> | ||
<constraint firstItem="R6c-5O-Ip8" firstAttribute="centerY" secondItem="O7j-Bf-ACJ" secondAttribute="centerY" id="FzZ-ed-C5P"/> | ||
<constraint firstItem="6zp-Vs-ovw" firstAttribute="leading" secondItem="R6c-5O-Ip8" secondAttribute="trailing" constant="3" id="NXq-7s-fxT"/> | ||
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="6zp-Vs-ovw" secondAttribute="trailing" constant="8" id="aqr-RD-inv"/> | ||
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="centerY" secondItem="6zp-Vs-ovw" secondAttribute="centerY" id="b9l-Fr-vO8"/> | ||
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="top" secondItem="m8k-6m-S8c" secondAttribute="top" constant="5" id="dpT-8x-DnB"/> | ||
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="centerY" secondItem="R6c-5O-Ip8" secondAttribute="centerY" id="f0l-31-lgq"/> | ||
<constraint firstItem="R6c-5O-Ip8" firstAttribute="leading" secondItem="O7j-Bf-ACJ" secondAttribute="trailing" constant="3" id="fJp-K3-wkR"/> | ||
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="leading" secondItem="m8k-6m-S8c" secondAttribute="leading" constant="8" id="ikq-q4-iQg"/> | ||
<constraint firstItem="R6c-5O-Ip8" firstAttribute="leading" secondItem="O7j-Bf-ACJ" secondAttribute="trailing" constant="3" id="onP-rZ-joU"/> | ||
</constraints> | ||
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/> | ||
<point key="canvasLocation" x="145" y="-203"/> | ||
</view> | ||
</objects> | ||
<simulatedMetricsContainer key="defaultSimulatedMetrics"> | ||
<simulatedStatusBarMetrics key="statusBar"/> | ||
<simulatedOrientationMetrics key="orientation"/> | ||
<simulatedScreenMetrics key="destination" type="retina4"/> | ||
</simulatedMetricsContainer> | ||
</document> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.