Skip to content

Commit

Permalink
Added label textColor animation
Browse files Browse the repository at this point in the history
  • Loading branch information
andreamazz committed Nov 9, 2014
1 parent 750e70e commit d69c234
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
40 changes: 30 additions & 10 deletions ViralSwitch/AMViralSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,21 @@ - (void)switchChanged:(UISwitch *)sender
[self.layer addAnimation:borderAnimation forKey:@"borderUp"];

for (NSDictionary *element in self.animationElementsOn) {
CABasicAnimation *elementAnimation = [self animateKeyPath:element[AMElementKeyPath]
fromValue:element[AMElementFromValue]
toValue:element[AMElementToValue]
timing:kCAMediaTimingFunctionEaseIn];
[element[AMElementView] addAnimation:elementAnimation forKey:element[AMElementKeyPath]];
if ([element[AMElementKeyPath] isEqualToString:@"textColor"] && [element[AMElementView] isKindOfClass:[UILabel class]]) {
[UIView transitionWithView:element[AMElementView]
duration:kDURATION
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[(UILabel *)element[AMElementView] setTextColor:element[AMElementToValue]];
}
completion:nil];
} else {
CABasicAnimation *elementAnimation = [self animateKeyPath:element[AMElementKeyPath]
fromValue:element[AMElementFromValue]
toValue:element[AMElementToValue]
timing:kCAMediaTimingFunctionEaseIn];
[element[AMElementView] addAnimation:elementAnimation forKey:element[AMElementKeyPath]];
}
}

} else {
Expand All @@ -92,11 +102,21 @@ - (void)switchChanged:(UISwitch *)sender
[self.layer addAnimation:borderAnimation forKey:@"borderDown"];

for (NSDictionary *element in self.animationElementsOff) {
CABasicAnimation *elementAnimation = [self animateKeyPath:element[AMElementKeyPath]
fromValue:element[AMElementFromValue]
toValue:element[AMElementToValue]
timing:kCAMediaTimingFunctionEaseIn];
[element[AMElementView] addAnimation:elementAnimation forKey:element[AMElementKeyPath]];
if ([element[AMElementKeyPath] isEqualToString:@"textColor"] && [element[AMElementView] isKindOfClass:[UILabel class]]) {
[UIView transitionWithView:element[AMElementView]
duration:kDURATION
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[(UILabel *)element[AMElementView] setTextColor:element[AMElementToValue]];
}
completion:nil];
} else {
CABasicAnimation *elementAnimation = [self animateKeyPath:element[AMElementKeyPath]
fromValue:element[AMElementFromValue]
toValue:element[AMElementToValue]
timing:kCAMediaTimingFunctionEaseIn];
[element[AMElementView] addAnimation:elementAnimation forKey:element[AMElementKeyPath]];
}
}

}
Expand Down
35 changes: 9 additions & 26 deletions ViralSwitchDemo/ViralSwitchDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,19 @@
<rect key="frame" x="263" y="126" width="51" height="31"/>
<color key="onTintColor" red="0.2901960784" green="0.56470588239999997" blue="0.8862745098" alpha="1" colorSpace="calibratedRGB"/>
</switch>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="O4d-Er-SEy">
<rect key="frame" x="20" y="127" width="223" height="30"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Turning blue" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="IZ8-Pn-04N">
<rect key="frame" x="63" y="3" width="98" height="24"/>
<fontDescription key="fontDescription" name="AvenirNext-Regular" family="Avenir Next" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="centerY" secondItem="IZ8-Pn-04N" secondAttribute="centerY" id="DG1-jW-lQc"/>
<constraint firstAttribute="height" constant="30" id="igb-XP-KBo"/>
<constraint firstAttribute="centerX" secondItem="IZ8-Pn-04N" secondAttribute="centerX" id="vT5-RU-qDR"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
<integer key="value" value="15"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Turning blue" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="IZ8-Pn-04N">
<rect key="frame" x="111" y="130" width="98" height="24"/>
<fontDescription key="fontDescription" name="AvenirNext-Regular" family="Avenir Next" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="0.95563052398989901" green="0.95563052398989901" blue="0.95563052398989901" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="centerY" secondItem="yke-CT-eZm" secondAttribute="centerY" id="85n-k4-Fjq"/>
<constraint firstItem="O4d-Er-SEy" firstAttribute="centerY" secondItem="yke-CT-eZm" secondAttribute="centerY" id="F4t-xk-PKe"/>
<constraint firstItem="IZ8-Pn-04N" firstAttribute="centerY" secondItem="yke-CT-eZm" secondAttribute="centerY" id="H7B-Cp-jGV"/>
<constraint firstAttribute="trailing" secondItem="yke-CT-eZm" secondAttribute="trailing" constant="8" id="bDH-NU-2Ry"/>
<constraint firstItem="yke-CT-eZm" firstAttribute="leading" secondItem="O4d-Er-SEy" secondAttribute="trailing" constant="20" id="gcK-EP-eX6"/>
<constraint firstItem="O4d-Er-SEy" firstAttribute="leading" secondItem="FpN-v3-W5p" secondAttribute="leading" constant="20" id="rmD-PX-XU3"/>
<constraint firstAttribute="centerX" secondItem="IZ8-Pn-04N" secondAttribute="centerX" id="qm3-2x-fq0"/>
</constraints>
</view>
<view clipsSubviews="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="MSy-qh-fHz">
Expand Down Expand Up @@ -110,8 +93,8 @@
</constraints>
</view>
<connections>
<outlet property="blueLabel" destination="IZ8-Pn-04N" id="6x5-cL-7jM"/>
<outlet property="blueSwitch" destination="yke-CT-eZm" id="91n-Hy-0HJ"/>
<outlet property="blueView" destination="O4d-Er-SEy" id="aqm-XY-kcJ"/>
<outlet property="greenSwitch" destination="7YS-VQ-nac" id="xxd-eO-FUq"/>
<outlet property="greenView" destination="ipx-l4-JZX" id="CRe-bk-bRt"/>
</connections>
Expand Down
20 changes: 8 additions & 12 deletions ViralSwitchDemo/ViralSwitchDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ @interface ViewController ()
@property (weak, nonatomic) IBOutlet AMViralSwitch *blueSwitch;
@property (weak, nonatomic) IBOutlet AMViralSwitch *greenSwitch;

@property (weak, nonatomic) IBOutlet UIView *blueView;
@property (weak, nonatomic) IBOutlet UIView *greenView;

@property (weak, nonatomic) IBOutlet UILabel *blueLabel;

@end

Expand All @@ -26,19 +25,16 @@ - (void)viewDidLoad {
[super viewDidLoad];

self.blueSwitch.animationElementsOn = @[
@{ AMElementView: self.blueView.layer,
AMElementKeyPath: @"backgroundColor",
AMElementFromValue: (id)[UIColor clearColor].CGColor,
AMElementToValue: (id)[UIColor whiteColor].CGColor }
@{ AMElementView: self.blueLabel,
AMElementKeyPath: @"textColor",
AMElementToValue: [UIColor whiteColor] }
];

self.blueSwitch.animationElementsOff = @[
@{ AMElementView: self.blueView.layer,
AMElementKeyPath: @"backgroundColor",
AMElementFromValue: (id)[UIColor whiteColor].CGColor,
AMElementToValue: (id)[UIColor clearColor].CGColor }
];

@{ AMElementView: self.blueLabel,
AMElementKeyPath: @"textColor",
AMElementToValue: [UIColor blackColor] }
];

self.greenSwitch.animationElementsOn = @[
@{ AMElementView: self.greenView.layer,
Expand Down

0 comments on commit d69c234

Please sign in to comment.